-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathdynamic-spec.js
88 lines (81 loc) · 2.67 KB
/
dynamic-spec.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
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
// @ts-check
// Type check works for JSON module
// because in "jsconfig.json" file I use "resolveJsonModule: true"
import { data } from '../fixtures/three.json'
import { createItems } from './utils'
import { deleteAll } from './utils-using-client'
// read the blog post https://glebbahmutov.com/blog/dynamic-tests-from-fixture/
// watch the video "Dynamic Tests From Cypress.io Fixture File" https://youtu.be/EXVwvJrUGJ8
describe('Creates each item', { tags: '@dynamic' }, () => {
beforeEach(deleteAll)
// use the imported items to create each item
// then verify the application shows it
it('in a single test', () => {
// clear all existing items
deleteAll()
cy.fixture('three.json')
.its('data.allTodos')
.then((list) => {
list.forEach((item) => {
// create the item using a network call
cy.request({
method: 'POST',
url: 'http://localhost:3000/',
body: {
operationName: 'AddTodo',
query: `
mutation AddTodo($title: String!, $completed: Boolean!) {
createTodo(title: $title, completed: $completed) {
id
}
}
`,
variables: {
title: item.title,
completed: item.completed,
},
},
})
// visit the page and check the item is present
cy.visit('/')
const classAssertion = item.completed
? 'have.class'
: 'not.have.class'
cy.contains('.todo', item.title).should(classAssertion, 'completed')
})
})
})
// create a test for each item imported from the fixture
data.allTodos.forEach((item, k) => {
it(`creates item ${k + 1}`, () => {
// create the item using a network call
cy.request({
method: 'POST',
url: 'http://localhost:3000/',
body: {
operationName: 'AddTodo',
query: `
mutation AddTodo($title: String!, $completed: Boolean!) {
createTodo(title: $title, completed: $completed) {
id
}
}
`,
variables: {
title: item.title,
completed: item.completed,
},
},
})
// visit the page and check the item is present
cy.visit('/')
const classAssertion = item.completed ? 'have.class' : 'not.have.class'
cy.contains('.todo', item.title).should(classAssertion, 'completed')
})
})
it('creates items', () => {
createItems(data.allTodos)
cy.visit('/')
cy.get('.todo').should('have.length', data.allTodos.length)
})
})