Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add search page cypress tests #621

Merged
merged 5 commits into from
Feb 2, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions e2e/fixtures/groupSearchGraphQL.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions e2e/fixtures/searchPageGraphQL.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion e2e/fixtures/subjectpageGraphQL.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion e2e/fixtures/topicpageGraphQL.json

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion e2e/integration/frontpage_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import { visitOptions } from '../support';

describe('Front page', () => {
beforeEach(() => {
cy.server();
cy.visit('/?disableSSR=true', visitOptions);
});
it('should have a list of valid links on front page', () => {
Expand Down
32 changes: 32 additions & 0 deletions e2e/integration/search_page_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright (c) 2021-present, NDLA.
*
* This source code is licensed under the GPLv3 license found in the
* LICENSE file in the root directory of this source tree.
*
*/

import { visitOptions } from '../support';

describe('Search page', () => {
beforeEach(() => {
cy.apiIntercept(
'POST',
'**/graphql',
['groupSearchGraphQL', 'searchPageGraphQL'],
['GroupSearch', 'searchPageQuery'],
);
});

it('contains search bar', () => {
cy.visit('/search/?disableSSR=true', visitOptions);
cy.apiwait(['@groupSearchGraphQL', '@searchPageGraphQL']);
cy.get('input').focus();
});

it('LTI contains search bar', () => {
cy.visit('/lti/?disableSSR=true', visitOptions);
cy.apiwait(['@groupSearchGraphQL', '@searchPageGraphQL']);
cy.get('input').focus();
});
});
3 changes: 1 addition & 2 deletions e2e/integration/subjects_page_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import { visitOptions } from '../support';

describe('Subjects page', () => {
beforeEach(() => {
cy.server();
cy.visit('/?disableSSR=true', visitOptions);

cy.apiroute('POST', '**/graphql', 'subjectpageGraphQL');
cy.apiIntercept('POST', '**/graphql', 'subjectpageGraphQL');
cy.get('[data-testid="category-list"] button:contains("Alle fag"):visible')
.click()
.get('a:contains("Medieuttrykk og mediesamfunnet")')
Expand Down
3 changes: 1 addition & 2 deletions e2e/integration/topic_menu_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ import { visitOptions } from '../support';

describe('Topic menu', () => {
beforeEach(() => {
cy.server();
cy.visit('/?disableSSR=true', visitOptions);

cy.apiroute('POST', '**/graphql', 'subjectpageGraphQL');
cy.apiIntercept('POST', '**/graphql', 'subjectpageGraphQL');
cy.get('[data-testid="category-list"] button:contains("Alle fag"):visible')
.click()
.get('a:contains("Markedsføring og ledelse 1")')
Expand Down
11 changes: 7 additions & 4 deletions e2e/integration/topic_page_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,27 @@ import { visitOptions } from '../support';

describe('Topic page', () => {
beforeEach(() => {
cy.server();
cy.visit('/?disableSSR=true', visitOptions);

cy.apiroute('POST', '**/graphql', 'subjectpageGraphQL');
cy.apiIntercept(
'POST',
'**/graphql',
['subjectpageGraphQL', 'topicpageGraphQL'],
['subjectPageQuery', 'topicQuery'],
);
cy.get('[data-testid="category-list"] button:contains("Alle fag"):visible')
.click()
.get('a:contains("Medieuttrykk og mediesamfunnet")')
.last()
.click({ force: true });
cy.apiwait('@subjectpageGraphQL');

cy.apiroute('POST', '**/graphql', 'topicpageGraphQL');
cy.get(
'[data-testid="nav-box-list"] li a:contains("Idéskaping og mediedesign")',
).click({
force: true,
});
cy.apiwait(['@topicpageGraphQL']);
cy.apiwait('@topicpageGraphQL');
});

it('contains article header and introduction', () => {
Expand Down
29 changes: 22 additions & 7 deletions e2e/support/commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,26 @@
// -- This is will overwrite an existing command --
// Cypress.Commands.overwrite("visit", (originalFn, url, options) => { ... })

Cypress.Commands.add('apiroute', (method, url, alias) => {
if (Cypress.env('USE_FIXTURES')) {
return cy.route(method, url, `fixture:${alias}`).as(alias);
}
return cy.route(method, url).as(alias);
});
// alias can be a a string for single requests or an array of strings for multiple requests
// Multiple requests also needs an array of GraphQL operation names to distinguish different requests
Cypress.Commands.add(
'apiIntercept',
(method, url, alias, operationNames = []) => {
cy.intercept(method, url, req => {
const reqOperationName = req.body[0].operationName;
if (!operationNames.length) {
req.alias = alias;
} else if (operationNames.includes(reqOperationName)) {
req.alias = alias[operationNames.indexOf(reqOperationName)];
}
if (Cypress.env('USE_FIXTURES')) {
req.reply({
fixture: req.alias,
});
}
});
},
);

const readResponseBody = body => {
const fr = new FileReader();
Expand All @@ -43,7 +57,8 @@ const readResponseBody = body => {
fr.onload = () => {
resolve(fr.result);
};
fr.readAsText(body);
const blob = new Blob([JSON.stringify(body)], { type: 'application/json' });
fr.readAsText(blob);
});
};

Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
"start-with-local-graphql-and-article-converter": "cross-env LOCAL_GRAPHQL_API=true RAZZLE_LOCAL_ARTICLE_CONVERTER=true razzle start",
"start-with-local-article-converter": "cross-env RAZZLE_LOCAL_ARTICLE_CONVERTER=true yarn start",
"start-prod": "cross-env NODE_ENV=production node build/server | bunyan",
"e2e": "cypress open",
"e2e-use-fixtures": "cross-env CYPRESS_USE_FIXTURES=true cypress open",
"e2e-record-fixtures": "cross-env CYPRESS_RECORD_FIXTURES=true cypress run",
"e2e": "BABEL_ENV=test cypress open",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hva skal denne være? BABEL_ENV eller NODE_ENV må være satt i cypress 6

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tja. Kva seier cypressdokumentasjonen?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ser ut som det er en bug faktisk: cypress-io/cypress#6755. Kan kanskje la det stå som det er inntil videre?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tror kanskje vi må bruke cross-env for å sette denne for at det skal fungere likt på windows?

"e2e-use-fixtures": "cross-env BABEL_ENV=test CYPRESS_USE_FIXTURES=true cypress open",
"e2e-record-fixtures": "cross-env BABEL_ENV=test CYPRESS_RECORD_FIXTURES=true cypress run",
"pree2e-ci": "cross-env NDLA_ENVIRONMENT=mock NODE_ENV=production node build/server &",
"e2e-ci": "cross-env CYPRESS_USE_FIXTURES=true cypress run --record --parallel",
"e2e-ci": "cross-env BABEL_ENV=test CYPRESS_USE_FIXTURES=true cypress run --record --parallel",
"now-build": "npm run build",
"now-start": "cross-env NODE_ENV=production NOW=true node build/server | bunyan",
"get-gql-schema-local": "apollo schema:download --endpoint=http://localhost:4000/graphql-api/graphql src/gqlSchema.json"
Expand All @@ -36,7 +36,7 @@
"@babel/plugin-proposal-optional-chaining": "^7.8.3",
"@ndla/scripts": "^0.7.4",
"chalk": "^2.4.2",
"cypress": "4.4.1",
"cypress": "6.3.0",
"eslint": "^5.14.1",
"eslint-config-ndla": "^1.2.1",
"eslint-plugin-graphql": "^3.0.3",
Expand Down