Skip to content

Commit e4c9d62

Browse files
committed
more tests
1 parent 0ac2500 commit e4c9d62

18 files changed

+2963
-13
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
},
3636
"homepage": "https://github.com/vuejs/composition-api#readme",
3737
"devDependencies": {
38-
"@types/jest": "^24.0.13",
38+
"@types/jest": "^25.2.1",
3939
"@types/node": "^12.0.2",
4040
"cross-env": "^5.2.0",
4141
"husky": "^2.7.0",

src/apis/computed.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export interface ComputedRef<T = any> extends WritableComputedRef<T> {
1212
readonly value: T;
1313
}
1414

15-
interface WritableComputedRef<T> extends Ref<T> {}
15+
export interface WritableComputedRef<T> extends Ref<T> {}
1616

1717
// read-only
1818
export function computed<T>(getter: Option<T>['get']): ComputedRef<T>;

test/globals.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
declare function waitForUpdate(cb: Function): Promise<any>;
2+
3+
declare interface Window {
4+
waitForUpdate(cb: Function): Promise<any>;
5+
}

test/helpers/mockWarn.ts

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
declare global {
2+
namespace jest {
3+
interface Matchers<R, T> {
4+
toHaveBeenWarned(): R;
5+
toHaveBeenWarnedLast(): R;
6+
toHaveBeenWarnedTimes(n: number): R;
7+
}
8+
}
9+
}
10+
11+
export const mockError = () => mockWarn(true);
12+
13+
export function mockWarn(asError = false) {
14+
expect.extend({
15+
toHaveBeenWarned(received: string) {
16+
asserted.add(received);
17+
const passed = warn.mock.calls.some(args => args[0].indexOf(received) > -1);
18+
if (passed) {
19+
return {
20+
pass: true,
21+
message: () => `expected "${received}" not to have been warned.`,
22+
};
23+
} else {
24+
const msgs = warn.mock.calls.map(args => args[0]).join('\n - ');
25+
return {
26+
pass: false,
27+
message: () =>
28+
`expected "${received}" to have been warned.\n\nActual messages:\n\n - ${msgs}`,
29+
};
30+
}
31+
},
32+
33+
toHaveBeenWarnedLast(received: string) {
34+
asserted.add(received);
35+
const passed = warn.mock.calls[warn.mock.calls.length - 1][0].indexOf(received) > -1;
36+
if (passed) {
37+
return {
38+
pass: true,
39+
message: () => `expected "${received}" not to have been warned last.`,
40+
};
41+
} else {
42+
const msgs = warn.mock.calls.map(args => args[0]).join('\n - ');
43+
return {
44+
pass: false,
45+
message: () =>
46+
`expected "${received}" to have been warned last.\n\nActual messages:\n\n - ${msgs}`,
47+
};
48+
}
49+
},
50+
51+
toHaveBeenWarnedTimes(received: string, n: number) {
52+
asserted.add(received);
53+
let found = 0;
54+
warn.mock.calls.forEach(args => {
55+
if (args[0].indexOf(received) > -1) {
56+
found++;
57+
}
58+
});
59+
60+
if (found === n) {
61+
return {
62+
pass: true,
63+
message: () => `expected "${received}" to have been warned ${n} times.`,
64+
};
65+
} else {
66+
return {
67+
pass: false,
68+
message: () => `expected "${received}" to have been warned ${n} times but got ${found}.`,
69+
};
70+
}
71+
},
72+
});
73+
74+
let warn: jest.SpyInstance;
75+
const asserted: Set<string> = new Set();
76+
77+
beforeEach(() => {
78+
asserted.clear();
79+
warn = jest.spyOn(console, asError ? 'error' : 'warn');
80+
warn.mockImplementation(() => {});
81+
});
82+
83+
afterEach(() => {
84+
const assertedArray = Array.from(asserted);
85+
const nonAssertedWarnings = warn.mock.calls
86+
.map(args => args[0])
87+
.filter(received => {
88+
return !assertedArray.some(assertedMsg => {
89+
return received.indexOf(assertedMsg) > -1;
90+
});
91+
});
92+
warn.mockRestore();
93+
if (nonAssertedWarnings.length) {
94+
nonAssertedWarnings.forEach(warning => {
95+
console.warn(warning);
96+
});
97+
throw new Error(`test case threw unexpected warnings.`);
98+
}
99+
});
100+
}

test/helpers/utils.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const Vue = require('vue/dist/vue.common.js');
2+
3+
import { waitForUpdate as wfu } from './wait-for-update';
4+
5+
export const waitForUpdate: (cb: Function) => Promise<any> = wfu;
6+
7+
export function nextTick(): Promise<any> {
8+
return Vue.nextTick();
9+
}

test/helpers/wait-for-update.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const Vue = require('vue');
1212
// // more assertions...
1313
// })
1414
// .then(done)
15+
1516
window.waitForUpdate = initialCb => {
1617
let end;
1718
const queue = initialCb ? [initialCb] : [];
@@ -68,6 +69,8 @@ window.waitForUpdate = initialCb => {
6869
return chainer;
6970
};
7071

72+
exports.waitForUpdate = window.waitForUpdate;
73+
7174
function timeout(n) {
7275
return next => setTimeout(next, n);
7376
}

0 commit comments

Comments
 (0)