This repository was archived by the owner on Feb 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathnew-post-spec.js
215 lines (173 loc) · 6.7 KB
/
new-post-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
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
/// <reference types="Cypress" />
import { title, about, article, tags } from '../fixtures/post'
import { stripIndent } from 'common-tags'
describe('New post', () => {
beforeEach(() => {
cy.task('cleanDatabase')
cy.registerUserIfNeeded()
cy.login()
})
it('writes a post', () => {
// I have added "data-cy" attributes
// following Cypress best practices
// https://on.cypress.io/best-practices#Selecting-Elements
cy.get('[data-cy=new-post]').click()
cy.get('[data-cy=title]').type('my title')
cy.get('[data-cy=about]').type('about X')
cy.get('[data-cy=article]').type('this post is **important**.')
cy.get('[data-cy=tags]').type('test{enter}')
cy.get('[data-cy=publish]').click()
// changed url means the post was successfully created
cy.location('pathname').should('equal', '/article/my-title')
})
it('can edit an article', () => {
cy.contains('a.nav-link', 'New Post').click()
// I have added "data-cy" attributes to select input fields
cy.get('[data-cy=title]').type('my title')
cy.get('[data-cy=about]').type('about X')
cy.get('[data-cy=article]').type('this post is **important**.')
cy.get('[data-cy=tags]').type('test{enter}')
cy.get('[data-cy=publish]').click()
cy.location('pathname').should('equal', '/article/my-title')
cy.get('[data-cy=edit-article]').click()
cy.location('pathname').should('equal', '/editor/my-title')
cy.get('[data-cy=title]')
.clear()
.type('a brand new title')
cy.get('[data-cy=publish]').click()
cy.location('pathname').should('equal', '/article/a-brand-new-title')
})
it('can fav and unfav an article', () => {
cy.contains('a.nav-link', 'New Post').click()
// I have added "data-cy" attributes to select input fields
cy.get('[data-cy=title]').type('my title')
cy.get('[data-cy=about]').type('about X')
cy.get('[data-cy=article]').type('this post is **important**.')
cy.get('[data-cy=tags]').type('test{enter}')
cy.get('[data-cy=publish]').click()
// wait for the article to be published
// otherwise if we just click on the profile link right away
// we might load profile - THEN immediately load the article
// because we clicked on it first
cy.location('pathname').should('equal', '/article/my-title')
cy.get('[data-cy=home]').click()
cy.get('[data-cy=global-feed]').click()
cy.get('.article-preview')
.should('have.length', 1)
.first()
.find('[data-cy=fav-article]')
.click()
// now go to my profile and see this article
cy.get('[data-cy=profile]').click()
cy.location('pathname').should('equal', '/@testuser')
cy.contains('.article-preview', 'my title')
// now unfav article
.find('[data-cy=fav-article]')
.click()
})
it('sets tags', () => {
cy.contains('a.nav-link', 'New Post').click()
// I have added "data-cy" attributes to select input fields
cy.get('[data-cy=title]').type('my title')
cy.get('[data-cy=about]').type('about X')
cy.get('[data-cy=article]').type('this post is **important**.')
const tags = ['code', 'testing', 'cypress.io']
cy.get('[data-cy=tags]').type(tags.join('{enter}') + '{enter}')
cy.get('[data-cy=publish]').click()
// check that each tag is displayed after post is shown
cy.url().should('match', /my-title$/)
tags.forEach(tag => cy.contains('.tag-default', tag))
})
it('sets the post body at once', () => {
cy.contains('a.nav-link', 'New Post').click()
// I have added "data-cy" attributes to select input fields
cy.get('[data-cy=title]').type('my title')
cy.get('[data-cy=about]').type('about X')
// to speed up creating the post, set the text as value
// and then trigger change event by typing "Enter"
const post = stripIndent`
# Fast tests
> Speed up your tests using direct access to DOM elements
You can set long text all at once and then trigger \`onChange\` event.
`
cy.get('[data-cy=article]')
.invoke('val', post)
.type('{enter}')
cy.get('[data-cy=tags]').type('test{enter}')
cy.get('[data-cy=publish]').click()
cy.contains('h1', 'my title')
})
it('adds a new post', () => {
cy.contains('a.nav-link', 'New Post').click()
// instead hard-coding text in this test
// the blog post contents comes from cypress/fixtures/post.js
cy.get('[data-cy=title]').type(title)
cy.get('[data-cy=about]').type(about)
// typing entire post as a human user takes too long
// just set it at once!
// instead of
// cy.get('[data-cy=article]').type(article)
// dispatch Redux actions
cy.window()
.its('store')
.invoke('dispatch', {
type: 'UPDATE_FIELD_EDITOR',
key: 'body',
value: article
})
// need to click "Enter" after each tag
cy.get('[data-cy=tags]').type(tags.join('{enter}') + '{enter}')
// and post the new article
cy.get('[data-cy=publish]').click()
// the url should show the new article
cy.url().should('include', '/article/' + Cypress._.kebabCase(title))
// new article should be on the server
cy.request('http://localhost:3000/api/articles?limit=10&offset=0')
.its('body')
.should(body => {
expect(body).to.have.property('articlesCount', 1)
expect(body.articles).to.have.length(1)
const firstPost = body.articles[0]
expect(firstPost).to.contain({
title,
description: about,
body: article
})
expect(firstPost.tagList).to.be.an('array')
const sortedTags = firstPost.tagList.sort()
expect(sortedTags).to.deep.equal(tags.sort())
})
})
it('deletes post', () => {
cy.contains('a.nav-link', 'New Post').click()
// instead hard-coding text in this test
// the blog post contents comes from cypress/fixtures/post.js
cy.get('[data-cy=title]').type(title)
cy.get('[data-cy=about]').type(about)
// dispatch Redux actions
cy.window()
.its('store')
.invoke('dispatch', {
type: 'UPDATE_FIELD_EDITOR',
key: 'body',
value: article
})
// need to click "Enter" after each tag
cy.get('[data-cy=tags]').type(tags.join('{enter}') + '{enter}')
// and post the new article
cy.get('[data-cy=publish]').click()
// the url should show the new article
cy.url().should('include', '/article/' + Cypress._.kebabCase(title))
cy.get('[data-cy=delete-article]').click()
// goes back to the main page
cy.location('pathname').should('equal', '/')
})
it('creates article using API', () => {
cy.postArticle({
title: 'first post',
description: 'first description',
body: 'first article',
tagList: ['first', 'testing']
})
})
})