-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Expand file tree
/
Copy pathedit.cy.js
More file actions
374 lines (317 loc) · 14.4 KB
/
edit.cy.js
File metadata and controls
374 lines (317 loc) · 14.4 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
import createPageFactory from '../support/CreatePage';
import editPageFactory from '../support/EditPage';
import listPageFactory from '../support/ListPage';
import loginPageFactory from '../support/LoginPage';
describe('Edit Page', () => {
const EditPostPage = editPageFactory('/#/posts/5');
const ListPagePosts = listPageFactory('/#/posts');
const CreatePostPage = createPageFactory('/#/posts/create');
const EditCommentPage = editPageFactory('/#/comments/5');
const ListCommentPage = listPageFactory('/#/comments');
const LoginPage = loginPageFactory('/#/login');
const EditUserPage = editPageFactory('/#/users/3');
const CreateUserPage = createPageFactory('/#/users/create');
describe('Title', () => {
it('should show the correct title in the appBar', () => {
EditPostPage.navigate();
cy.get(EditPostPage.elements.title).contains(
'Post "Sed quo et et fugiat modi"'
);
});
});
describe('TabbedForm', () => {
beforeEach(() => EditPostPage.navigate());
it('should display the title in a TextField', () => {
cy.get(EditPostPage.elements.input('title')).should(el =>
expect(el).to.have.value('Sed quo et et fugiat modi')
);
});
it('should allow to update elements', () => {
// For some unknown reason, the click on submit didn't work in cypress
// so we submit with enter
EditPostPage.setInputValue('input', 'title', 'Lorem Ipsum{enter}');
// Ensure react-admin has handled the update as it will redirect to the list page
// once done
cy.url().should('match', /\/#\/posts$/);
EditPostPage.navigate();
cy.get(EditPostPage.elements.input('title')).should(el =>
expect(el).to.have.value('Lorem Ipsum')
);
});
it('should redirect to list page after edit success', () => {
// For some unknown reason, the click on submit didn't work in cypress
// so we submit with enter
EditPostPage.setInputValue(
'input',
'title',
'Lorem Ipsum again{enter}'
);
cy.url().should('match', /\/#\/posts$/);
});
it('should allow to switch tabs', () => {
EditPostPage.gotoTab(3);
cy.get(EditPostPage.elements.input('average_note')).should(el =>
expect(el).to.have.value('3')
);
});
it('should keep DateInput value after opening datapicker', () => {
EditPostPage.gotoTab(3);
const date = new Date('2012-08-05').toISOString().slice(0, 10);
cy.get(EditPostPage.elements.input('published_at')).should(el =>
expect(el).to.have.value(date)
);
EditPostPage.clickInput('published_at');
cy.get(EditPostPage.elements.input('published_at')).should(el =>
expect(el).to.have.value(date)
);
});
it('should validate inputs inside ArrayInput', () => {
EditPostPage.gotoTab(3);
cy.get(EditPostPage.elements.addBacklinkButton).click();
EditPostPage.clickInput('backlinks.0.url');
cy.get(EditPostPage.elements.input('backlinks.0.url')).blur();
EditPostPage.submit();
cy.contains('Required');
// FIXME: We navigate away from the page and confirm the unsaved changes
// This is needed because HashHistory would prevent further navigation
cy.window().then(() => {
cy.on('window:confirm', () => true);
});
cy.get('.RaSidebar-fixed [role="menuitem"]:first-child').click();
});
it('should change reference list correctly when changing filter', () => {
const EditPostTagsPage = editPageFactory('/#/posts/13');
EditPostTagsPage.navigate();
EditPostTagsPage.gotoTab(3);
cy.wait(250);
// Music is selected by default
cy.get(
EditPostTagsPage.elements.input('tags', 'reference-array-input')
).within(() => {
cy.get(`[role=button]`).contains('Music').should('exist');
});
EditPostTagsPage.clickInput('change-filter');
// Music should not be selected anymore after filter reset
cy.get(
EditPostTagsPage.elements.input('tags', 'reference-array-input')
).within(() => {
cy.get(`[role=button]`).should('not.exist');
});
cy.get(
EditPostTagsPage.elements.input('tags', 'reference-array-input')
).within(() => {
cy.get(`input`).click();
});
// Music should not be visible in the list after filter reset
cy.get('[role="listbox"]').within(() => {
cy.contains('Music').should('not.exist');
});
cy.get('[role="listbox"]').within(() => {
cy.contains('Photo').should('exist');
});
});
});
it('should fill form correctly even when switching from one form type to another', () => {
EditCommentPage.navigate();
cy.get(EditPostPage.elements.input('author.name')).should(el =>
expect(el).to.have.value('Edmond Schulist')
);
// This validates that the current form values are not kept after we navigate
EditCommentPage.setInputValue('input', 'body', 'Test');
cy.on('window:confirm', message => {
expect(message).to.equal(
"Some of your changes weren't saved. Are you sure you want to ignore them?"
);
});
// FIXME
// We can't navigate using cypress function as it would prevent the confirm dialog
// to appear. This is because react-router (history) cannot block history pushes that
// it didn't initiate.
cy.contains('Create post').click();
cy.get(CreatePostPage.elements.input('body', 'rich-text-input')).should(
el => expect(el.text()).to.equal('')
);
});
it('should allow to select an item from the AutocompleteInput without showing the choices again after', () => {
EditCommentPage.navigate();
cy.get(
'[value="Accusantium qui nihil voluptatum quia voluptas maxime ab similique - 1"]'
);
cy.wait(500);
cy.get(EditCommentPage.elements.input('post_id'))
.type('{selectall}')
.clear()
.type('Sed quo');
cy.contains('[role="option"]', 'Sed quo et et fugiat modi').click();
cy.get('[role="option"]').should(el => expect(el).to.not.exist);
// Ensure it does not reappear a little after
cy.wait(500);
cy.get('[role="option"]').should(el => expect(el).to.not.exist);
// Ensure they still appear when needed though
cy.get(EditCommentPage.elements.input('post_id'))
.clear()
.type('Accusantium qui nihil');
// We select the original value so that the form stay pristine and we avoid the
// warning about unsaved changes that prevents the following tests to run
cy.contains(
'[role="option"]',
'Accusantium qui nihil voluptatum quia voluptas maxime ab similique'
).click();
});
it('should reset the form correctly when switching from edit to create', () => {
EditPostPage.navigate();
cy.get(EditPostPage.elements.input('title')).should(el =>
expect(el).to.have.value('Sed quo et et fugiat modi')
);
// This validates that the current form values are not kept after we navigate
EditPostPage.setInputValue('input', 'title', 'Another title');
cy.on('window:confirm', message => {
expect(message).to.equal(
"Some of your changes weren't saved. Are you sure you want to ignore them?"
);
});
// FIXME
// We can't navigate using cypress function as it would prevent the confirm dialog
// to appear. This is because react-router (history) cannot block history pushes that
// it didn't initiate.
cy.contains('Create').click();
cy.get(CreatePostPage.elements.input('title')).should(el =>
expect(el).to.have.value('')
);
// This validates the old record values are not kept after we navigated
const currentDate = new Date();
const currentDateString = currentDate.toISOString().slice(0, 10);
cy.get(CreatePostPage.elements.input('published_at')).should(el =>
expect(el).to.have.value(currentDateString)
);
});
it('should intialize the form correctly when cloning from edit', () => {
EditPostPage.navigate();
cy.get(EditPostPage.elements.input('title')).should(el =>
expect(el).to.have.value('Sed quo et et fugiat modi')
);
EditPostPage.clone();
cy.url().then(url => expect(url).to.contain('/#/posts/create'));
cy.get(CreatePostPage.elements.input('title')).should(el =>
expect(el).to.have.value('Sed quo et et fugiat modi')
);
const date = new Date('2012-08-05').toISOString().slice(0, 10);
cy.get(CreatePostPage.elements.input('published_at')).should(el =>
expect(el).to.have.value(date)
);
});
it('should not revert values when saving a record that was cloned', () => {
EditPostPage.navigate();
cy.get(EditPostPage.elements.input('title')).should(el =>
expect(el).to.have.value('Sed quo et et fugiat modi')
);
EditPostPage.clone();
CreatePostPage.setInputValue('input', 'title', 'Lorem Ipsum');
// The next assertion has to occur immediately, thus CreatePostPage.submit() is not used
cy.get(CreatePostPage.elements.submitButton).click();
cy.get(CreatePostPage.elements.input('title')).then(el => {
expect(el).to.have.value('Lorem Ipsum');
});
});
it('should not lose the cloned values when switching tabs', () => {
EditPostPage.navigate();
EditPostPage.logout();
LoginPage.navigate();
LoginPage.login('admin', 'password');
EditUserPage.navigate();
cy.get(EditUserPage.elements.input('name')).should(el =>
expect(el).to.have.value('Annamarie Mayer')
);
EditUserPage.clone();
cy.get(CreateUserPage.elements.input('name')).then(el => {
expect(el).to.have.value('Annamarie Mayer');
});
CreateUserPage.gotoTab(2);
CreateUserPage.gotoTab(1);
cy.get(CreateUserPage.elements.input('name')).then(el => {
expect(el).to.have.value('Annamarie Mayer');
});
});
it('should save edited user values', () => {
EditPostPage.navigate();
EditPostPage.logout();
LoginPage.navigate();
LoginPage.login('admin', 'password');
EditUserPage.navigate();
cy.get(EditUserPage.elements.input('name')).should(el =>
expect(el).to.have.value('Annamarie Mayer')
);
EditUserPage.setInputValue('textbox', 'name', 'Annamarie Mayer!');
EditUserPage.submit();
EditUserPage.navigate();
cy.get(EditUserPage.elements.input('name')).should(el =>
expect(el).to.have.value('Annamarie Mayer!')
);
});
it('should persit emptied inputs', () => {
EditPostPage.navigate();
EditPostPage.gotoTab(3);
cy.contains('Tech').click();
cy.get('li[aria-label="Clear value"]').click();
EditPostPage.setInputValue('input', 'average_note', '{enter}', false);
cy.url().should('match', /\/#\/posts$/);
ListPagePosts.waitUntilDataLoaded();
EditPostPage.navigate();
EditPostPage.gotoTab(3);
cy.get(EditPostPage.elements.input('category')).should(el =>
expect(el).to.have.value('')
);
});
it('should refresh the list when the update fails', () => {
ListPagePosts.navigate();
ListPagePosts.nextPage(); // Ensure the record is visible in the table
cy.contains('Sed quo et et fugiat modi'); // wait for data
EditPostPage.navigate();
EditPostPage.setInputValue('input', 'title', 'f00bar');
EditPostPage.submit();
ListPagePosts.waitUntilDataLoaded();
cy.get(ListPagePosts.elements.recordRows)
.eq(2)
.should(el => expect(el).to.contain('f00bar'));
cy.get('body').click('left'); // dismiss notification
cy.get('div[role="alert"]').should(el =>
expect(el).to.have.text('The form is invalid')
);
cy.get(ListPagePosts.elements.recordRows)
.eq(2)
.should(el => expect(el).to.contain('Sed quo et et fugiat modi'));
});
it('should not display a warning about unsaved changes when an array input has been updated', () => {
ListPagePosts.navigate();
ListPagePosts.nextPage(); // Ensure the record is visible in the table
EditPostPage.navigate();
// Select first notification input checkbox
cy.get(
EditPostPage.elements.input('notifications', 'checkbox-group-input')
)
.eq(0)
.click();
EditPostPage.submit();
// If the update succeeded without display a warning about unsaved changes,
// we should have been redirected to the list
cy.url().then(url => expect(url).to.contain('/#/posts'));
});
describe('lifecycle callbacks', () => {
it('should delete related comments when deleting a post', () => {
ListCommentPage.navigate();
ListCommentPage.waitUntilDataLoaded();
cy.get('[data-testid="postLink"]').should('have.length', 6);
ListCommentPage.nextPage();
ListCommentPage.waitUntilDataLoaded();
cy.get('[data-testid="postLink"]').should('have.length', 5);
EditPostPage.navigate();
EditPostPage.delete();
ListCommentPage.navigate(); // go back to the list, on page 2
ListCommentPage.waitUntilDataLoaded();
cy.get('[data-testid="postLink"]').should('have.length', 3);
ListCommentPage.previousPage();
ListCommentPage.waitUntilDataLoaded();
cy.get('[data-testid="postLink"]').should('have.length', 6);
});
});
});