This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathfollow-user-spec.js
87 lines (82 loc) · 2.75 KB
/
follow-user-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
// We use "cy.api" custom command that comes from a plugin.
// We should load "@bahmutov/cy-api" TypeScript definition,
// which will in turn load Cypress TypeScript definition.
/// <reference types="@bahmutov/cy-api" />
describe('following user', () => {
const secondUser = {
username: 'seconduser',
image: 'https://robohash.org/MQ9.png?set=set3&size=150x150',
email: '[email protected]',
password: 'seconduser'
}
beforeEach(() => {
cy.task('cleanDatabase')
cy.registerUserIfNeeded(secondUser)
cy.registerUserIfNeeded()
})
it('can follow second user', () => {
// the second user should write a post
cy.login(secondUser)
cy.contains('[data-cy=profile]', secondUser.username).should('be.visible')
// write post
cy.contains('a.nav-link', '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()
// 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')
// log out
cy.contains('[data-cy=profile]', secondUser.username).click()
cy.get('[data-cy="edit-profile-settings"]')
.should('be.visible')
.click()
cy.get('[data-cy=logout]').click()
cy.get('[data-cy=sign-in]').should('be.visible')
// login as our test user
cy.login()
cy.get('[data-cy=global-feed]').click()
cy.get('.article-preview')
.should('have.length', 1)
.first()
.find('.author')
.click()
// we should get to the user profile page
cy.location('pathname').should('equal', '/@seconduser')
cy.get('[data-cy=follow-unfollow-user]')
.should(c => {
expect(c.text()).to.include('Follow seconduser')
})
.click()
// unfollow user
cy.get('[data-cy=follow-unfollow-user]')
.should(c => {
expect(c.text()).to.include('Unfollow seconduser')
})
.click()
})
it('user cannot follow themselves', () => {
const user = Cypress.env('user')
cy.getLoginToken(user).then(token => {
const apiUrl = Cypress.env('apiUrl')
cy.api({
method: 'POST',
url: `${apiUrl}/api/profiles/${user.username}/follow`,
headers: {
authorization: `Token ${token}`
},
failOnStatusCode: false // we expect failure
})
.its('body')
.should('deep.equal', {
errors: {
forbidden: ['You cannot follow yourself']
}
})
})
})
})