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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199import { render, screen, waitFor, fireEvent } from '@testing-library/vue'
import i18n from './utils/utils'
import Subscription from '../pages/subscription/components/Subscription.vue'
import axios from 'axios'
import type { Category, SubscriptionPayload } from '../pages/subscription/types/types'
const realAxiosGet = axios.get
jest.mock('axios')
const mockedAxios = axios as jest.Mocked<typeof axios>;
const alertMock = jest.spyOn(window, 'alert').mockImplementation();
const BASE_URL = 'http://localhost:3000/v1'
const initGetCategoriesMock = (status: number, categories: Category[] | null, error: Error | null = null) => {
mockedAxios.get.mockImplementationOnce((url: string, config) => {
if (url === `${BASE_URL}/categories`) {
return status === 200 ? Promise.resolve({ data: { categories } }) : Promise.reject(error);
}
return realAxiosGet(url, config)
})
}
const initCreateSubscriptionMock = (status: number, payload: SubscriptionPayload, error: Error | null = null) => {
mockedAxios.post.mockImplementationOnce((url: string, data: unknown, config) => {
if (url === `${BASE_URL}/subscriptions` && JSON.stringify(data) === JSON.stringify(payload)) {
return status === 200 ? Promise.resolve({ data: null }) : Promise.reject(error);
}
return realAxiosGet(url, config)
})
}
const renderComponent = () => {
return render(Subscription, {
global: {
plugins: [i18n]
}
})
}
const mockCategories = [
{ guid: '123-456-789', name: 'Product updates' },
{ guid: '234-567-890', name: 'Market insights' }
]
describe('Subscription Page', () => {
beforeEach(() => {
mockedAxios.get.mockReset();
mockedAxios.post.mockReset();
alertMock.mockRestore();
})
afterAll(() => {
mockedAxios.get.mockReset();
mockedAxios.post.mockReset();
alertMock.mockRestore();
})
it('should render correctly and display the form when the category list was fetched successfully', async () => {
initGetCategoriesMock(200,mockCategories);
renderComponent();
expect(screen.getByPlaceholderText('First name')).toBeInTheDocument();
expect(screen.getByPlaceholderText('Last name')).toBeInTheDocument();
expect(screen.getByPlaceholderText('Work Email')).toBeInTheDocument();
await waitFor(() => {
expect(screen.getByText('Newsletter Categories')).toBeInTheDocument();
expect(screen.getByText('Product updates')).toBeInTheDocument();
expect(alertMock).not.toHaveBeenCalled();
})
})
it('should display an error alert when the category list was not fetched successfully', async () => {
initGetCategoriesMock(500, null, new Error('Failed to fetch categories'));
renderComponent();
expect(screen.getByPlaceholderText('First name')).toBeInTheDocument();
expect(screen.getByPlaceholderText('Last name')).toBeInTheDocument();
expect(screen.getByPlaceholderText('Work Email')).toBeInTheDocument();
await waitFor(() => {
expect(alertMock).toHaveBeenCalledWith('Failed to fetch categories, please try again');
})
})
it('should display validation error when first name or last name is empty', async () => {
initGetCategoriesMock(200, mockCategories);
renderComponent()
await waitFor(() => {
expect(screen.getByText('Newsletter Categories')).toBeInTheDocument();
expect(screen.getByText('Product updates')).toBeInTheDocument();
})
await fireEvent.update(screen.getByPlaceholderText('First name'), '')
await fireEvent.blur(screen.getByPlaceholderText('First name'))
await fireEvent.update(screen.getByPlaceholderText('Last name'), '')
await fireEvent.blur(screen.getByPlaceholderText('Last name'))
await waitFor(() => {
expect(screen.getByText('firstName is required')).toBeInTheDocument()
expect(screen.getByText('lastName is required')).toBeInTheDocument()
})
})
it('should display validation error when email is invalid', async () => {
initGetCategoriesMock(200, mockCategories);
renderComponent()
await waitFor(() => {
expect(screen.getByText('Newsletter Categories')).toBeInTheDocument();
expect(screen.getByText('Product updates')).toBeInTheDocument();
})
await fireEvent.update(screen.getByPlaceholderText('First name'), 'John')
await fireEvent.update(screen.getByPlaceholderText('Last name'), 'Doe')
await fireEvent.update(screen.getByPlaceholderText('Work Email'), 'invalid-email')
await fireEvent.blur(screen.getByPlaceholderText('Work Email'))
await waitFor(() => {
expect(screen.queryByText('firstName is required')).not.toBeInTheDocument()
expect(screen.queryByText('lastName is required')).not.toBeInTheDocument()
expect(screen.getByText('Please enter a valid email address')).toBeInTheDocument()
})
})
it('should display validation error when at least one category is not selected', async () => {
initGetCategoriesMock(200, mockCategories);
renderComponent()
await waitFor(() => {
expect(screen.getByText('Newsletter Categories')).toBeInTheDocument();
expect(screen.getByText('Product updates')).toBeInTheDocument();
})
await fireEvent.update(screen.getByPlaceholderText('First name'), 'John')
await fireEvent.update(screen.getByPlaceholderText('Last name'), 'Doe')
await fireEvent.update(screen.getByPlaceholderText('Work Email'), 'johndoe@example.com')
const submitButton = screen.getByText('Subscribe')
await fireEvent.click(submitButton)
await waitFor(() => {
expect(screen.queryByText('firstName is required')).not.toBeInTheDocument()
expect(screen.queryByText('lastName is required')).not.toBeInTheDocument()
expect(screen.queryByText('Please enter a valid email address')).not.toBeInTheDocument()
expect(screen.getByText('Please select at least one category')).toBeInTheDocument()
})
})
it('should display success alert when the subscription creation for the customer was successful', async () => {
initGetCategoriesMock(200, mockCategories);
initCreateSubscriptionMock(200, {
first_name: 'John',
last_name: 'Doe',
work_email: 'john@example.com',
category_guids: ['123-456-789']
});
renderComponent()
await waitFor(() => {
expect(screen.getByText('Newsletter Categories')).toBeInTheDocument();
expect(screen.getByText('Product updates')).toBeInTheDocument();
})
await fireEvent.update(screen.getByPlaceholderText('First name'), 'John')
await fireEvent.update(screen.getByPlaceholderText('Last name'), 'Doe')
await fireEvent.update(screen.getByPlaceholderText('Work Email'), 'john@example.com')
const checkbox = screen.getByLabelText('Product updates')
await fireEvent.click(checkbox)
await fireEvent.click(screen.getByText('Subscribe'))
await waitFor(() => {
expect(alertMock).toHaveBeenCalledWith('Subscription created successfully')
})
})
it('should display error alert when the subscription creation for the customer failed', async () => {
initGetCategoriesMock(200, mockCategories);
initCreateSubscriptionMock(500, {
first_name: 'John',
last_name: 'Doe',
work_email: 'john@example.com',
category_guids: ['123-456-789']
}, new Error('Failed to create subscription'));
renderComponent()
await waitFor(() => {
expect(screen.getByText('Newsletter Categories')).toBeInTheDocument();
expect(screen.getByText('Product updates')).toBeInTheDocument();
})
await fireEvent.update(screen.getByPlaceholderText('First name'), 'John')
await fireEvent.update(screen.getByPlaceholderText('Last name'), 'Doe')
await fireEvent.update(screen.getByPlaceholderText('Work Email'), 'john@example.com')
const checkbox = screen.getByLabelText('Product updates')
await fireEvent.click(checkbox)
await fireEvent.click(screen.getByText('Subscribe'))
await waitFor(() => {
expect(alertMock).not.toHaveBeenCalledWith('Subscription created successfully')
expect(alertMock).toHaveBeenCalledWith('Failed to create subscription, please try again')
})
})
})