import { createStore, setActiveReq } from '../src'
describe('Getters', () => {
+ jest.useFakeTimers()
+
const useStore = () => {
// create a new store
setActiveReq({})
id: 'main',
state: () => ({
name: 'Eduardo',
+ forCallCheck: 'foo',
+ callCount: 0,
}),
getters: {
upperCaseName() {
return this.name.toUpperCase()
},
+ // works for js users but cannot be typed at the same time as `this`
+ // so it will have to be explicitly typed by the user
+ // https://github.com/posva/pinia/issues/249
+ callCheck(state: any) {
+ setImmediate(() => {
+ // avoid getting tracked
+ state.callCount++
+ })
+ return state.forCallCheck
+ },
doubleName() {
return this.upperCaseName
},
store.name = 'Ed'
expect(store.composed).toBe('ED: ok')
})
+
+ it('computes getters correctly', async () => {
+ const store = useStore()
+ expect(store.callCount).toBe(0)
+ expect(store.callCheck).toBe('foo')
+ expect(store.callCount).toBe(0)
+ jest.runAllImmediates()
+ expect(store.callCount).toBe(1)
+ expect(store.callCheck).toBe('foo')
+ jest.runAllImmediates()
+ expect(store.callCount).toBe(1)
+ expect(store.callCheck).toBe('foo')
+ // changing a different value on the store
+ store.name = 'Ed'
+ jest.runAllImmediates()
+ expect(store.callCount).toBe(1)
+ expect(store.callCheck).toBe('foo')
+ })
})
for (const getterName in getters) {
computedGetters[getterName] = computed(() => {
setActiveReq(_r)
+ // we could also pass state.value instead of the store as the first
+ // argument but the later is more flexible for JS users while TS users
+ // will like to use `this` to get all correct typings
// eslint-disable-next-line @typescript-eslint/no-use-before-define
return getters[getterName].call(store, store)
}) as StoreWithGetters<G>[typeof getterName]