forked from algolia/vue-instantsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjest.setup.js
55 lines (49 loc) · 1.31 KB
/
jest.setup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import createSerializer from 'jest-serializer-html/createSerializer';
import { isVue3, isVue2, Vue2 } from './src/util/vue-compat';
if (isVue2) {
Vue2.config.productionTip = false;
}
expect.addSnapshotSerializer(
createSerializer({
print: {
sortAttributes: names => names.sort(),
},
})
);
const toHaveEmptyHTML = wrapper => {
const html = wrapper.html();
if (
(isVue2 && html === '') ||
(isVue3 && ['<!---->', '<!--v-if-->'].includes(html))
) {
return {
pass: true,
};
} else {
return {
pass: false,
message: () => `expected ${html} to be an empty HTML string`,
};
}
};
const toHaveBooleanAttribute = attribute => wrapper => {
// :hidden="true" becomes
// hidden="hidden" in Vue 2 and
// hidden="" in Vue 3.
// So we need this to write correct tests to match them in both versions.
const value = wrapper.attributes(attribute);
if ((isVue2 && value === attribute) || (isVue3 && value === '')) {
return { pass: true };
} else {
return {
pass: false,
message: () => `expected ${wrapper} to have \`${attribute}\` attribute`,
};
}
};
expect.extend({
toHaveEmptyHTML,
toBeDisabled: toHaveBooleanAttribute('disabled'),
toBeHidden: toHaveBooleanAttribute('hidden'),
toBeAutofocused: toHaveBooleanAttribute('autofocus'),
});