Skip to content

Commit 030b371

Browse files
committed
add: renderless dialog mvp specs
I mean, it's not the best coverage, but it's better than zero.
1 parent c408053 commit 030b371

File tree

1 file changed

+179
-0
lines changed

1 file changed

+179
-0
lines changed
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
import { mount, shallowMount } from "@vue/test-utils";
2+
import A11yVueDialogRenderless from "../A11yVueDialogRenderless.vue";
3+
4+
describe("A11yVueDialogRenderless", () => {
5+
const stubbedClick = jest.fn()
6+
const methodsMock = {
7+
close: jest.fn(),
8+
handleKeyboard: jest.fn(),
9+
_stopPropagation: jest.fn(),
10+
trapFocus: jest.fn()
11+
}
12+
13+
/**
14+
* https://vue-test-utils.vuejs.org/api/options.html#scopedslots
15+
*/
16+
const WrapperComp = {
17+
components: {
18+
A11yVueDialogRenderless
19+
},
20+
data:() => ({
21+
isOpen: false,
22+
}),
23+
template: `
24+
<A11yVueDialogRenderless
25+
:open="isOpen"
26+
@close="$emit('close')"
27+
#default="{ open, backdropRef, dialogRef, titleRef, closeRef }"
28+
>
29+
<portal to="a11y-vue-dialogs" v-if="open">
30+
<div
31+
class="mock-dialog"
32+
v-bind="backdropRef.props"
33+
v-on="backdropRef.listeners"
34+
>
35+
<div
36+
class="mock-dialog__inner"
37+
v-bind="dialogRef.props"
38+
v-on="dialogRef.listeners"
39+
>
40+
<h1
41+
class="mock-dialog__title"
42+
v-bind="titleRef.props"
43+
>
44+
Mock title
45+
</h1>
46+
47+
<button
48+
class="mock-dialog__close"
49+
v-bind="closeRef.props"
50+
v-on="closeRef.listeners"
51+
>
52+
Mock close
53+
</button>
54+
</div>
55+
</div>
56+
</portal>
57+
</A11yVueDialogRenderless>
58+
`
59+
};
60+
61+
const wrapper = mount(WrapperComp, {
62+
attachToDocument: true,
63+
stubs: {
64+
portal: true
65+
}
66+
}).find(A11yVueDialogRenderless);
67+
68+
const openWrapper = mount(WrapperComp, {
69+
attachToDocument: true,
70+
data() {
71+
return {
72+
isOpen: true
73+
}
74+
},
75+
stubs: {
76+
portal: true
77+
}
78+
}).find(A11yVueDialogRenderless);
79+
80+
openWrapper.setMethods(methodsMock);
81+
82+
// Sets spies on console object to make it possible to convert them
83+
// into test failures.
84+
// const spyError = jest.spyOn(console, 'error')
85+
// const spyWarn = jest.spyOn(console, 'warn')
86+
87+
// beforeEach(() => {
88+
// spyError.mockReset()
89+
// spyWarn.mockReset()
90+
// })
91+
92+
// sanity check
93+
it("is a Vue instance", () => {
94+
expect(wrapper.isVueInstance()).toBeTruthy();
95+
});
96+
97+
describe('props', () => {
98+
it('should have "open" prop set to false by default', () => {
99+
expect(wrapper.props('open')).toBe(false)
100+
})
101+
102+
it('should have "role" prop set to "dialog" by default', () => {
103+
expect(wrapper.props('role')).toBe('dialog')
104+
})
105+
106+
it('should have "preventBackgroundScrolling" prop set to "true" by default', () => {
107+
expect(wrapper.props('preventBackgroundScrolling')).toBe(true)
108+
})
109+
110+
it('displays markup when it’s open', () => {
111+
expect(openWrapper.props('open')).toBe(true);
112+
expect(openWrapper.find('.mock-dialog').exists()).toBeTruthy()
113+
})
114+
})
115+
116+
describe('bindings', () => {
117+
describe('backdropRef', () => {
118+
const backdropRef = openWrapper.find('.mock-dialog')
119+
120+
it('should attach correct binding props to bound element', () => {
121+
expect(backdropRef.attributes('data-ref')).toBe('backdrop')
122+
expect(backdropRef.attributes('tabindex')).toBe('-1')
123+
expect(backdropRef.attributes('data-id')).toContain(`a11y-vue-dialog-`)
124+
})
125+
126+
it('should attach correct binding listeners to bound element', () => {
127+
backdropRef.trigger('click')
128+
129+
expect(methodsMock.close).toHaveBeenCalled();
130+
})
131+
})
132+
133+
describe('dialogRef', () => {
134+
const dialogRef = openWrapper.find('.mock-dialog__inner')
135+
136+
it('should attach correct binding props to bound element', () => {
137+
expect(dialogRef.attributes('data-ref')).toBe('dialog')
138+
expect(dialogRef.attributes('role')).toBe('dialog')
139+
expect(dialogRef.attributes('aria-labelledby')).toContain(`a11y-vue-dialog-`)
140+
})
141+
142+
it('should attach correct binding listeners to bound element', async () => {
143+
dialogRef.trigger('click')
144+
dialogRef.trigger('keydown.tab')
145+
dialogRef.trigger('keydown.esc')
146+
147+
await openWrapper.vm.$nextTick()
148+
149+
expect(methodsMock._stopPropagation).toHaveBeenCalled();
150+
expect(methodsMock.handleKeyboard).toHaveBeenCalledTimes(2);
151+
})
152+
})
153+
154+
describe('closeRef', () => {
155+
const closeRef = openWrapper.find('.mock-dialog__close')
156+
157+
it('should attach correct binding props to bound element', () => {
158+
expect(closeRef.attributes('data-ref')).toBe('close')
159+
})
160+
161+
it('should attach correct binding listeners to bound element', async () => {
162+
closeRef.trigger('click')
163+
164+
await openWrapper.vm.$nextTick()
165+
166+
expect(methodsMock.close).toHaveBeenCalled();
167+
})
168+
})
169+
170+
describe('titleRef', () => {
171+
const titleRef = openWrapper.find('.mock-dialog__title')
172+
173+
console.log(openWrapper.html())
174+
it('should attach correct binding props to bound element', () => {
175+
expect(titleRef.attributes('id')).toContain('-title')
176+
})
177+
})
178+
})
179+
});

0 commit comments

Comments
 (0)