|
| 1 | +import { createTestingPinia, TestingOptions } from './testing' |
| 2 | +import { defineStore } from 'pinia' |
| 3 | +import { mount } from '@vue/test-utils' |
| 4 | +import { defineComponent } from 'vue' |
| 5 | + |
| 6 | +describe('Testing: initial state', () => { |
| 7 | + const useCounter = defineStore('counter', { |
| 8 | + state: () => ({ n: 0, nested: { n: 0, other: false } }), |
| 9 | + actions: { |
| 10 | + increment(amount = 1) { |
| 11 | + this.n += amount |
| 12 | + }, |
| 13 | + }, |
| 14 | + }) |
| 15 | + |
| 16 | + const Counter = defineComponent({ |
| 17 | + setup() { |
| 18 | + const counter = useCounter() |
| 19 | + return { counter } |
| 20 | + }, |
| 21 | + template: ` |
| 22 | + <button @click="counter.increment()">+1</button> |
| 23 | + <span>{{ counter.n }}</span> |
| 24 | + <button @click="counter.increment(10)">+10</button> |
| 25 | + `, |
| 26 | + }) |
| 27 | + |
| 28 | + function factory(options?: TestingOptions) { |
| 29 | + const wrapper = mount(Counter, { |
| 30 | + global: { |
| 31 | + plugins: [createTestingPinia(options)], |
| 32 | + }, |
| 33 | + }) |
| 34 | + |
| 35 | + const counter = useCounter() |
| 36 | + |
| 37 | + return { wrapper, counter } |
| 38 | + } |
| 39 | + |
| 40 | + it('can set an initial state', () => { |
| 41 | + const { counter } = factory({ |
| 42 | + initialState: { counter: { n: 10 } }, |
| 43 | + }) |
| 44 | + expect(counter.nested).toEqual({ n: 0, other: false }) |
| 45 | + expect(counter.n).toBe(10) |
| 46 | + counter.n++ |
| 47 | + expect(counter.n).toBe(11) |
| 48 | + }) |
| 49 | + |
| 50 | + it('can provide objects', () => { |
| 51 | + const { counter } = factory({ |
| 52 | + initialState: { counter: { nested: { n: 10 } } }, |
| 53 | + }) |
| 54 | + expect(counter.n).toBe(0) |
| 55 | + expect(counter.nested.other).toBe(false) |
| 56 | + expect(counter.nested.n).toBe(10) |
| 57 | + counter.nested.n++ |
| 58 | + expect(counter.nested.n).toBe(11) |
| 59 | + }) |
| 60 | +}) |
0 commit comments