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

New: Cypress task log added, cypress commands file added, cypress get… #3489

Merged
merged 11 commits into from
Jan 11, 2024
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"commonjs": false,
"es2022": true,
"amd": true,
"jest": true
"jest": true,
"cypress": true
},
"extends": [
"standard",
Expand Down
12 changes: 10 additions & 2 deletions cypress.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ module.exports = defineConfig({
baseUrl: 'http://localhost:9001/',
screenshotOnRunFailure: false,
video: false,
supportFile: false,
specPattern: '**/test/e2e/**/*.cy.{js,jsx}'
supportFile: '**/test/e2e/commands.js',
specPattern: '**/test/e2e/**/*.cy.{js,jsx}',
setupNodeEvents (on, config) {
on('task', {
log(message) {
console.log(message);
return null;
}
});
}
}
});
2 changes: 1 addition & 1 deletion grunt/helpers/Data.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class Data {
});
language.load();
return language;
});
}).filter(lang => lang.isValid);
this.configFile = new JSONFile({
framework: this.framework,
path: path.join(coursePath, `config.${this.jsonext}`)
Expand Down
9 changes: 6 additions & 3 deletions grunt/helpers/data/Language.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,18 +107,21 @@ class Language {
return index;
}, {});

this.getCourseFileItem();

return this;
}

/** @type {boolean} */
get isValid() {
return Boolean(this.courseFileItem);
}

/** @type {boolean} */
get hasChanged() {
return this.files.some(file => file.hasChanged);
}

/**
* Produces a manifest file for the Framework data layer at course/language_data_manifest.js.
* Produces a manifest file for the Framework data layer at course/lang/language_data_manifest.js.
* @returns {Language}
*/
saveManifest() {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,10 @@ async function waitForGruntServer() {

async function cypressRun() {
if (argumentValues.testfiles) {
return asyncSpawn('node', './node_modules/cypress/bin/cypress', 'run', '--spec', `${argumentValues.testfiles}`);
return asyncSpawn('node', './node_modules/cypress/bin/cypress', 'run', '--spec', `${argumentValues.testfiles}`, '--config', `{"fixturesFolder": "${argumentValues.outputdir}"}`);
}

return asyncSpawn('node', './node_modules/cypress/bin/cypress', 'run');
return asyncSpawn('node', './node_modules/cypress/bin/cypress', 'run', '--config', `{"fixturesFolder": "${argumentValues.outputdir}"}`);
};

async function jestRun() {
Expand Down
109 changes: 109 additions & 0 deletions test/e2e/commands.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
function getBuild() {
try {
return cy.fixture(`adapt/js/build.min.js`).then(build => {
// Return for cy.getBuild().then(build => {});
// Expose this.build in cypress
return cy.wrap(build).as('build');
});
} catch {
cy.task('log', 'fail');
}
}

function getConfig() {
return getBuild().then(build => {
// Load the config.json
return cy.fixture(`${build.coursedir}/config.json`).then(config => {
// Return for cy.getConfig().then(config => {});
// Expose this.config in cypress
return cy.wrap(config).as('config');
});
});
}

function getData(languageCode = null) {
try {
// Setup data array
const data = [];
// Expose this.data in cypress
cy.wrap(data).as('data');
// Allow adapt-style shorthand properties:
// this.data.course, this.data.contentObjects, this.data.articles, etc
Object.defineProperties(data, {
course: {
get() {
return data.find(item => item._type === 'course');
},
enumerable: false
},
contentObjects: {
get() {
return data.filter(item => ['menu','page'].includes(item._type));
},
enumerable: false
},
articles: {
get() {
return data.filter(item => item._type === 'article');
},
enumerable: false
},
blocks: {
get() {
return data.filter(item => item._type === 'block');
},
enumerable: false
},
components: {
get() {
return data.filter(item => item._type === 'component');
},
enumerable: false
}
});
return getBuild().then(build => {
const {
coursedir,
availableLanguageNames
} = build;
// Load the config.json
return getConfig().then(config => {
// Check that the specified language is available
const defaultLanguage = config._defaultLanguage;
languageCode = languageCode ?? defaultLanguage;
if (!availableLanguageNames.includes(languageCode)) {
throw new Error(`Language code is not available: ${languageCode}`);
}
// Load the language_data_manifest.js for the default or specified language
cy.fixture(`${coursedir}/${languageCode}/language_data_manifest.js`).then(languageDataManifest => {
// Load each of the files specified in the manifest
languageDataManifest.forEach(localFilePath => {
const filePath = `${coursedir}/${languageCode}/${localFilePath}`
cy.fixture(filePath).then(fileData => {
// Add __index__ and __path__ attributes to each object as in adapt
// so that each object's origin can be identified later if necessary
if (Array.isArray(fileData)) {
fileData.forEach((item, index) => {
item.__index__ = index;
item.__path__ = filePath;
});
data.push(...fileData);
return;
}
fileData.__path__ = filePath;
data.push(fileData);
});
});
});
// Return for cy.getData(languageCode).then(data => {});
return cy.wrap(data);
});
});
} catch {
cy.task('log', 'fail');
}
}

Cypress.Commands.add('getBuild', getBuild);
Cypress.Commands.add('getConfig', getConfig);
Cypress.Commands.add('getData', getData);
11 changes: 11 additions & 0 deletions test/e2e/config.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
describe('Config', function () {

beforeEach(function () {
cy.getConfig();
});

it('should have a valid direction', function () {
expect(this.config._defaultDirection).to.be.oneOf(['ltr', 'rtl']);
});

});
17 changes: 17 additions & 0 deletions test/e2e/languages.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
describe('Languages', function () {

beforeEach(function () {
cy.getConfig().then(config => cy.getData(config._defaultLanguage));
});

it('should have the default language', function () {
expect(this.build.availableLanguageNames).to.include(this.config._defaultLanguage);
});

it('should have data for all specified languages', function () {
this.build.availableLanguageNames.forEach(lang => {
cy.getData(lang);
});
});

});
8 changes: 0 additions & 8 deletions test/e2e/menuPage.cy.js

This file was deleted.

22 changes: 22 additions & 0 deletions test/e2e/trackingIds.cy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
describe('Tracking Ids', function () {

beforeEach(function () {
cy.getBuild();
});

it('should have specified tracking ids for all specified languages', function () {
const {
trackingIdType,
availableLanguageNames
} = this.build;
availableLanguageNames.forEach(lang => {
cy.getData(lang).then(data => {
const trackingIdItems = data.filter(item => item._type === trackingIdType);
trackingIdItems.forEach(item => {
expect(item).to.have.ownProperty('_trackingId');
});
})
});
});

});