|
1 | | -import { mount } from '@vue/test-utils'; |
| 1 | +import { render, screen } from '@testing-library/vue'; |
| 2 | +import userEvent from '@testing-library/user-event'; |
| 3 | +import { createLocalVue } from '@vue/test-utils'; |
| 4 | +import Vuex, { Store } from 'vuex'; |
| 5 | +import VueRouter from 'vue-router'; |
2 | 6 | import UserPrivilegeModal from '../UserPrivilegeModal'; |
3 | 7 |
|
4 | | -const userId = 'test-user-id'; |
5 | | -const currentEmail = '[email protected]'; |
| 8 | +const localVue = createLocalVue(); |
| 9 | +localVue.use(Vuex); |
| 10 | +localVue.use(VueRouter); |
6 | 11 |
|
7 | | -function makeWrapper(props = {}) { |
8 | | - return mount(UserPrivilegeModal, { |
9 | | - propsData: { |
10 | | - userId, |
11 | | - value: true, |
12 | | - ...props, |
13 | | - }, |
14 | | - computed: { |
15 | | - user() { |
16 | | - return { |
17 | | - id: userId, |
18 | | - }; |
19 | | - }, |
20 | | - currentEmail() { |
21 | | - return currentEmail; |
| 12 | +const currentEmail = '[email protected]'; |
| 13 | + |
| 14 | +const createMockStore = () => { |
| 15 | + return new Store({ |
| 16 | + state: { |
| 17 | + session: { |
| 18 | + currentUser: { |
| 19 | + email: currentEmail, |
| 20 | + }, |
22 | 21 | }, |
23 | 22 | }, |
24 | 23 | }); |
25 | | -} |
| 24 | +}; |
| 25 | + |
| 26 | +const renderComponent = (props = {}) => { |
| 27 | + const confirmAction = jest.fn(() => Promise.resolve()); |
| 28 | + const store = createMockStore(); |
| 29 | + |
| 30 | + const defaultProps = { |
| 31 | + value: true, |
| 32 | + title: 'Add admin privileges', |
| 33 | + text: 'Are you sure you want to add admin privileges to user ...?', |
| 34 | + confirmText: 'Add privileges', |
| 35 | + confirmAction, |
| 36 | + ...props, |
| 37 | + }; |
| 38 | + |
| 39 | + return { |
| 40 | + ...render(UserPrivilegeModal, { |
| 41 | + localVue, |
| 42 | + store, |
| 43 | + routes: new VueRouter(), |
| 44 | + props: defaultProps, |
| 45 | + }), |
| 46 | + confirmAction, |
| 47 | + }; |
| 48 | +}; |
26 | 49 |
|
27 | 50 | describe('userPrivilegeModal', () => { |
28 | | - let wrapper; |
29 | | - let confirmAction; |
| 51 | + it('should render modal with correct title, text, and confirm button label', () => { |
| 52 | + renderComponent(); |
30 | 53 |
|
31 | | - beforeEach(() => { |
32 | | - confirmAction = jest.fn(); |
33 | | - wrapper = makeWrapper({ confirmAction }); |
| 54 | + expect(screen.getByText('Add admin privileges')).toBeInTheDocument(); |
| 55 | + expect( |
| 56 | + screen.getByText('Are you sure you want to add admin privileges to user ...?'), |
| 57 | + ).toBeInTheDocument(); |
| 58 | + expect(screen.getByText('Add privileges')).toBeInTheDocument(); |
34 | 59 | }); |
35 | 60 |
|
36 | | - it('clicking cancel should reset the values', async () => { |
37 | | - wrapper = makeWrapper({ emailConfirm: 'testing' }); |
38 | | - wrapper.findComponent('[data-test="cancel"]').trigger('click'); |
39 | | - await wrapper.vm.$nextTick(); |
40 | | - expect(wrapper.vm.emailConfirm).toBe(''); |
41 | | - }); |
| 61 | + describe('clicking cancel', () => { |
| 62 | + let user; |
| 63 | + let confirmAction; |
| 64 | + let emitted; |
42 | 65 |
|
43 | | - it('submitting form should call confirm', async () => { |
44 | | - const confirm = jest.spyOn(wrapper.vm, 'confirm'); |
45 | | - confirm.mockImplementation(() => {}); |
46 | | - wrapper.findComponent({ ref: 'form' }).trigger('submit'); |
47 | | - await wrapper.vm.$nextTick(); |
48 | | - expect(confirm).toHaveBeenCalled(); |
| 66 | + beforeEach(async () => { |
| 67 | + user = userEvent.setup(); |
| 68 | + const result = renderComponent(); |
| 69 | + confirmAction = result.confirmAction; |
| 70 | + emitted = result.emitted; |
| 71 | + |
| 72 | + const cancelButton = screen.getByRole('button', { name: /cancel/i }); |
| 73 | + await user.click(cancelButton); |
| 74 | + }); |
| 75 | + |
| 76 | + it('should not call confirmAction', () => { |
| 77 | + expect(confirmAction).not.toHaveBeenCalled(); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should emit input event with false', () => { |
| 81 | + expect(emitted().input[0]).toEqual([false]); |
| 82 | + }); |
49 | 83 | }); |
50 | 84 |
|
51 | | - it('confirm should not call confirmAction if emailConfirm is blank', async () => { |
52 | | - wrapper.vm.confirm(); |
53 | | - await wrapper.vm.$nextTick(); |
54 | | - expect(confirmAction).not.toHaveBeenCalled(); |
| 85 | + describe('submitting the form with empty e-mail', () => { |
| 86 | + let user; |
| 87 | + let confirmAction; |
| 88 | + let emitted; |
| 89 | + |
| 90 | + beforeEach(async () => { |
| 91 | + user = userEvent.setup(); |
| 92 | + const result = renderComponent(); |
| 93 | + confirmAction = result.confirmAction; |
| 94 | + emitted = result.emitted; |
| 95 | + |
| 96 | + const submitButton = screen.getByRole('button', { name: /add privileges/i }); |
| 97 | + await user.click(submitButton); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should show validation error', () => { |
| 101 | + expect(screen.getByText('Email does not match your account email')).toBeInTheDocument(); |
| 102 | + }); |
| 103 | + |
| 104 | + it('should not call confirmAction', () => { |
| 105 | + expect(confirmAction).not.toHaveBeenCalled(); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should not emit input event', () => { |
| 109 | + expect(emitted().input).toBeUndefined(); |
| 110 | + }); |
55 | 111 | }); |
56 | 112 |
|
57 | | - it('confirm should not call confirmAction if emailConfirm is not correct', async () => { |
58 | | - await wrapper.setData({ emailConfirm: '[email protected]' }); |
59 | | - wrapper.vm.confirm(); |
60 | | - await wrapper.vm.$nextTick(); |
61 | | - expect(confirmAction).not.toHaveBeenCalled(); |
| 113 | + describe('submitting the form with an e-mail different from the current e-mail', () => { |
| 114 | + let user; |
| 115 | + let confirmAction; |
| 116 | + let emitted; |
| 117 | + |
| 118 | + beforeEach(async () => { |
| 119 | + user = userEvent.setup(); |
| 120 | + const result = renderComponent(); |
| 121 | + confirmAction = result.confirmAction; |
| 122 | + emitted = result.emitted; |
| 123 | + |
| 124 | + const emailInput = screen.getByLabelText(/email address/i); |
| 125 | + await user.type(emailInput, '[email protected]'); |
| 126 | + |
| 127 | + const submitButton = screen.getByRole('button', { name: /add privileges/i }); |
| 128 | + await user.click(submitButton); |
| 129 | + }); |
| 130 | + |
| 131 | + it('should show validation error', () => { |
| 132 | + expect(screen.getByText('Email does not match your account email')).toBeInTheDocument(); |
| 133 | + }); |
| 134 | + |
| 135 | + it('should not call confirmAction', () => { |
| 136 | + expect(confirmAction).not.toHaveBeenCalled(); |
| 137 | + }); |
| 138 | + |
| 139 | + it('should not emit input event', () => { |
| 140 | + expect(emitted().input).toBeUndefined(); |
| 141 | + }); |
62 | 142 | }); |
63 | 143 |
|
64 | | - it('confirm should call confirmAction if form is valid', async () => { |
65 | | - await wrapper.setData({ emailConfirm: currentEmail }); |
66 | | - wrapper.vm.confirm(); |
67 | | - await wrapper.vm.$nextTick(); |
68 | | - expect(confirmAction).toHaveBeenCalled(); |
| 144 | + describe('submitting the form with the current e-mail', () => { |
| 145 | + let user; |
| 146 | + let confirmAction; |
| 147 | + let emitted; |
| 148 | + |
| 149 | + beforeEach(async () => { |
| 150 | + user = userEvent.setup(); |
| 151 | + const result = renderComponent(); |
| 152 | + confirmAction = result.confirmAction; |
| 153 | + emitted = result.emitted; |
| 154 | + |
| 155 | + const emailInput = screen.getByLabelText(/email address/i); |
| 156 | + await user.type(emailInput, currentEmail); |
| 157 | + |
| 158 | + const submitButton = screen.getByRole('button', { name: /add privileges/i }); |
| 159 | + await user.click(submitButton); |
| 160 | + }); |
| 161 | + |
| 162 | + it('should not show validation error', () => { |
| 163 | + expect(screen.queryByText('Email does not match your account email')).not.toBeInTheDocument(); |
| 164 | + }); |
| 165 | + |
| 166 | + it('should call confirmAction', () => { |
| 167 | + expect(confirmAction).toHaveBeenCalled(); |
| 168 | + }); |
| 169 | + |
| 170 | + it('should emit input event with false', () => { |
| 171 | + expect(emitted().input[0]).toEqual([false]); |
| 172 | + }); |
69 | 173 | }); |
70 | 174 | }); |
0 commit comments