This repository has been archived by the owner on Nov 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 707
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve cypress-client integration and do more things the cypress way
- Loading branch information
Kent C. Dodds
committed
Feb 24, 2018
1 parent
02c79ec
commit 93fe350
Showing
10 changed files
with
151 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,49 @@ | ||
import {loginAsNewUser, logout, generate, assertRoute} from '../utils' | ||
import {generate} from '../utils' | ||
|
||
describe('authentication', () => { | ||
describe('posts', () => { | ||
let user | ||
beforeEach(() => { | ||
return logout() | ||
return cy | ||
.loginAsNewUser() | ||
.then(u => (user = u)) | ||
.visit('/') | ||
}) | ||
|
||
it('should allow existing users to login', () => { | ||
const fakePost = generate.postData() | ||
// shorten the content so we don't have to wait so long | ||
fakePost.content = fakePost.content.slice(0, 50) | ||
loginAsNewUser().then(user => { | ||
cy | ||
.getByTestId('create-post-link') | ||
.click() | ||
.getByTestId('title-input') | ||
.type(fakePost.title) | ||
.getByTestId('content-input') | ||
// the delay is because the content takes | ||
// forever to type otherwise | ||
.type(fakePost.content, {delay: 1}) | ||
.getByTestId('tags-input') | ||
.type(fakePost.tags.join(', ')) | ||
.getByTestId('editor-submit') | ||
.click() | ||
.assertRoute('/') | ||
cy | ||
.getByTestId('post-title') | ||
.first() | ||
.should('contain', fakePost.title) | ||
cy | ||
.getByTestId('post-content') | ||
.first() | ||
.should('contain', fakePost.content) | ||
fakePost.tags.forEach((t, i) => { | ||
cy | ||
.visitApp() | ||
.getByTestId('create-post-link') | ||
.click() | ||
.getByTestId('title-input') | ||
.type(fakePost.title) | ||
.getByTestId('content-input') | ||
// the delay is because the content takes | ||
// forever to type otherwise | ||
.type(fakePost.content, {delay: 1}) | ||
.getByTestId('tags-input') | ||
.type(fakePost.tags.join(', ')) | ||
.getByTestId('editor-submit') | ||
.click() | ||
assertRoute('/') | ||
cy | ||
.getByTestId('post-title') | ||
.first() | ||
.should('contain', fakePost.title) | ||
cy | ||
.getByTestId('post-content') | ||
.first() | ||
.should('contain', fakePost.content) | ||
fakePost.tags.forEach((t, i) => { | ||
cy | ||
.getByTestId(`post-tag-${i}`) | ||
.first() | ||
.should('contain', t) | ||
}) | ||
cy | ||
.getByTestId('post-author-username') | ||
.getByTestId(`post-tag-${i}`) | ||
.first() | ||
.should('contain', user.username) | ||
.should('contain', t) | ||
}) | ||
cy | ||
.getByTestId('post-author-username') | ||
.first() | ||
.should('contain', user.username) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,34 @@ | ||
Cypress.Commands.add('visitApp', route => { | ||
const clientUrl = Cypress.env('CLIENT_URL').trim() | ||
const apiUrl = Cypress.env('API_URL').trim() | ||
const query = `?api-url=${encodeURIComponent(apiUrl)}` | ||
const fullUrl = [clientUrl, route, query].filter(Boolean).join('/') | ||
return cy.visit(fullUrl) | ||
}) | ||
import {generate} from '../utils' | ||
|
||
Cypress.Commands.add('getByTestId', id => { | ||
return cy.get(`[data-test="${id}"]`) | ||
}) | ||
|
||
Cypress.Commands.add('logout', () => { | ||
return cy | ||
.window() | ||
.its('localStorage') | ||
.invoke('removeItem', 'token') | ||
}) | ||
|
||
Cypress.Commands.add('createNewUser', () => { | ||
const user = generate.loginForm() | ||
|
||
return cy | ||
.log('create a test new user', user) | ||
.request('POST', `${Cypress.env('API_URL').trim()}/auth/register`, user) | ||
.then(({body}) => { | ||
return Object.assign({}, body.user, user) | ||
}) | ||
}) | ||
|
||
Cypress.Commands.add('loginAsNewUser', () => { | ||
return cy.createNewUser().then(user => { | ||
window.localStorage.setItem('token', user.token) | ||
return user | ||
}) | ||
}) | ||
|
||
Cypress.Commands.add('assertRoute', route => { | ||
cy.url().should('equal', `${window.location.origin}${route}`) | ||
}) |
Oops, something went wrong.