Skip to content

Commit 6ae3224

Browse files
committed
Test the log in user flow
1 parent db0f616 commit 6ae3224

File tree

9 files changed

+682
-25
lines changed

9 files changed

+682
-25
lines changed

cypress.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"baseUrl": "http://localhost:3000"
3+
}

cypress/fixtures/example.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"name": "Using fixtures to represent data",
3+
"email": "hello@cypress.io",
4+
"body": "Fixtures are a great way to mock data for responses to routes"
5+
}

cypress/integration/auth.spec.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* eslint-disable no-undef */
2+
import { Default as TaskListDefault } from '../../src/components/TaskList.stories';
3+
4+
describe('The Login Page', () => {
5+
beforeEach(() => {
6+
cy.intercept('POST', '/authenticate', {
7+
statusCode: 201,
8+
body: {
9+
user: {
10+
name: 'Alice Carr',
11+
token:
12+
'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c',
13+
},
14+
},
15+
});
16+
17+
cy.intercept('GET', '/tasks', {
18+
statusCode: 201,
19+
body: TaskListDefault.args,
20+
});
21+
});
22+
23+
it('user can authenticate using the login form', () => {
24+
const email = 'alice.carr@test.com';
25+
const password = 'k12h1k0$5;lpa@Afn';
26+
27+
cy.visit('/');
28+
29+
// Fill out the form
30+
cy.get('input[name=email]').type(email);
31+
cy.get('input[name=password]').type(`${password}`);
32+
33+
// Click the sign-in button
34+
cy.get('button[type=submit]').click();
35+
36+
// UI should display the user's task list
37+
cy.get('[aria-label="tasks"] li').should('have.length', 6);
38+
});
39+
});

cypress/plugins/index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/// <reference types="cypress" />
2+
// ***********************************************************
3+
// This example plugins/index.js can be used to load plugins
4+
//
5+
// You can change the location of this file or turn off loading
6+
// the plugins file with the 'pluginsFile' configuration option.
7+
//
8+
// You can read more here:
9+
// https://on.cypress.io/plugins-guide
10+
// ***********************************************************
11+
12+
// This function is called when a project is opened or re-opened (e.g. due to
13+
// the project's config changing)
14+
15+
/**
16+
* @type {Cypress.PluginConfig}
17+
*/
18+
// eslint-disable-next-line no-unused-vars
19+
module.exports = (on, config) => {
20+
// `on` is used to hook into various events Cypress emits
21+
// `config` is the resolved Cypress config
22+
}

cypress/support/commands.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// ***********************************************
2+
// This example commands.js shows you how to
3+
// create various custom commands and overwrite
4+
// existing commands.
5+
//
6+
// For more comprehensive examples of custom
7+
// commands please read more here:
8+
// https://on.cypress.io/custom-commands
9+
// ***********************************************
10+
//
11+
//
12+
// -- This is a parent command --
13+
// Cypress.Commands.add('login', (email, password) => { ... })
14+
//
15+
//
16+
// -- This is a child command --
17+
// Cypress.Commands.add('drag', { prevSubject: 'element'}, (subject, options) => { ... })
18+
//
19+
//
20+
// -- This is a dual command --
21+
// Cypress.Commands.add('dismiss', { prevSubject: 'optional'}, (subject, options) => { ... })
22+
//
23+
//
24+
// -- This will overwrite an existing command --
25+
// Cypress.Commands.overwrite('visit', (originalFn, url, options) => { ... })

cypress/support/index.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// ***********************************************************
2+
// This example support/index.js is processed and
3+
// loaded automatically before your test files.
4+
//
5+
// This is a great place to put global configuration and
6+
// behavior that modifies Cypress.
7+
//
8+
// You can change the location of this file or turn off
9+
// automatically serving support files with the
10+
// 'supportFile' configuration option.
11+
//
12+
// You can read more here:
13+
// https://on.cypress.io/configuration
14+
// ***********************************************************
15+
16+
// Import commands.js using ES2015 syntax:
17+
import './commands'
18+
19+
// Alternatively you can use CommonJS syntax:
20+
// require('./commands')

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323
"eject": "react-scripts eject",
2424
"storybook": "start-storybook -p 6006",
2525
"build-storybook": "build-storybook",
26-
"test-storybook": "test-storybook"
26+
"test-storybook": "test-storybook",
27+
"cypress": "cypress open"
2728
},
2829
"eslintConfig": {
2930
"extends": [
@@ -68,6 +69,7 @@
6869
"@storybook/test-runner": "^0.1.0",
6970
"@storybook/testing-library": "^0.0.11",
7071
"axe-playwright": "^1.1.11",
72+
"cypress": "^9.6.1",
7173
"msw": "^0.40.0",
7274
"msw-storybook-addon": "^1.6.3"
7375
},

src/LoginScreen.stories.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import React from 'react';
2+
import { rest } from 'msw';
3+
import { LoginScreen } from './LoginScreen';
4+
import { Default as TaskListDefault } from './components/TaskList.stories';
5+
6+
export default {
7+
component: LoginScreen,
8+
title: 'LoginScreen',
9+
parameters: {
10+
backgrounds: { default: 'white' },
11+
},
12+
argTypes: {
13+
onLogIn: { action: 'onLogIn' },
14+
},
15+
};
16+
17+
const Template = (args) => <LoginScreen {...args} />;
18+
19+
export const Default = Template.bind({});
20+
Default.parameters = {
21+
msw: [
22+
rest.get('/tasks', (req, res, ctx) => {
23+
return res(ctx.json(TaskListDefault.args));
24+
}),
25+
],
26+
};

0 commit comments

Comments
 (0)