Skip to content

Commit c36dfef

Browse files
ericsorensonhenri-hulski
authored andcommitted
fix(overmind-vue): replace stopTracking with subscribe and add test coverage
Replace the removed track(onUpdate)/stopTracking() API calls with the current track()/subscribe() pattern, matching overmind-react. Fix track() call order so scoped state hooks correctly record path dependencies. Add 22 tests covering all exports with 100% code coverage.
1 parent de0fe8a commit c36dfef

File tree

8 files changed

+775
-133
lines changed

8 files changed

+775
-133
lines changed

package-lock.json

Lines changed: 339 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
"@types/vscode": "^1.70.0",
6868
"@types/ws": "^8.18.1",
6969
"@vscode/vsce": "3.7.1",
70+
"@vue/test-utils": "2.4.6",
7071
"babel-loader": "10.1.1",
7172
"babel-plugin-macros": "^3.1.0",
7273
"commitizen": "^4.3.1",

packages/overmind-vue/jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
module.exports = {
2+
testEnvironment: 'jsdom',
23
collectCoverage: true,
34
collectCoverageFrom: ['src/**/*.{t,j}s?(x)', '!src/**/*.d.ts'],
45
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json'],

packages/overmind-vue/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"build:es": "tsc --outDir es --module ES2022 --target ES2022",
1818
"clean": "rimraf es lib coverage",
1919
"typecheck": "tsc --noEmit",
20+
"test": "jest --runInBand",
2021
"test:watch": "jest --watch --updateSnapshot --coverage false",
2122
"prebuild": "npm run clean",
2223
"postbuild": "rimraf {lib,es}/**/__tests__"
@@ -36,6 +37,9 @@
3637
"overmind": "next",
3738
"tslib": "^2.8.1"
3839
},
40+
"devDependencies": {
41+
"@vue/test-utils": "^2.4.6"
42+
},
3943
"peerDependencies": {
4044
"vue": "^3.0.0"
4145
}

packages/overmind-vue/src/Index.old_test.ts

Lines changed: 0 additions & 70 deletions
This file was deleted.
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
jest.mock('overmind', () => {
2+
const actual = jest.requireActual('overmind')
3+
return {
4+
...actual,
5+
ENVIRONMENT: 'production',
6+
}
7+
})
8+
9+
import { Overmind } from 'overmind'
10+
import { defineComponent, h, nextTick } from 'vue'
11+
import { mount, flushPromises } from '@vue/test-utils'
12+
13+
import { withOvermind, createStateHook } from './'
14+
15+
const config = {
16+
state: {
17+
foo: 'bar',
18+
},
19+
actions: {
20+
doThis: ({ state }: any) => {
21+
state.foo = 'bar2'
22+
},
23+
},
24+
}
25+
26+
function mountWithOvermind(app: any, setup: () => () => any) {
27+
const Comp = defineComponent({ setup })
28+
return mount(withOvermind(app, Comp))
29+
}
30+
31+
describe('Vue (production mode)', () => {
32+
test('should render and re-render state', async () => {
33+
const app = new Overmind(config)
34+
let renderCount = 0
35+
36+
const wrapper = mountWithOvermind(app, () => {
37+
const state = createStateHook()()
38+
return () => {
39+
renderCount++
40+
return h('div', {}, (state.value as any).foo)
41+
}
42+
})
43+
44+
expect(renderCount).toBe(1)
45+
expect(wrapper.text()).toBe('bar')
46+
47+
app.actions.doThis()
48+
await flushPromises()
49+
await nextTick()
50+
51+
expect(renderCount).toBe(2)
52+
expect(wrapper.text()).toBe('bar2')
53+
})
54+
55+
test('should re-render scoped state', async () => {
56+
const app = new Overmind(config)
57+
58+
const wrapper = mountWithOvermind(app, () => {
59+
const state = createStateHook()((s: any) => ({ myFoo: s.foo }))
60+
return () => h('div', {}, (state.value as any).myFoo)
61+
})
62+
63+
expect(wrapper.text()).toBe('bar')
64+
65+
app.actions.doThis()
66+
await flushPromises()
67+
await nextTick()
68+
69+
expect(wrapper.text()).toBe('bar2')
70+
})
71+
72+
test('should dispose tree on unmount without devtools events', () => {
73+
const app = new Overmind(config)
74+
const disposeSpy = jest.spyOn(
75+
(app as any).proxyStateTreeInstance,
76+
'disposeTree'
77+
)
78+
const emitSpy = jest.spyOn(app.eventHub, 'emitAsync')
79+
80+
const wrapper = mountWithOvermind(app, () => {
81+
const state = createStateHook()()
82+
return () => h('div', {}, (state.value as any).foo)
83+
})
84+
85+
emitSpy.mockClear()
86+
wrapper.unmount()
87+
88+
expect(disposeSpy).toHaveBeenCalledTimes(1)
89+
expect(emitSpy).not.toHaveBeenCalled()
90+
})
91+
})

0 commit comments

Comments
 (0)