Skip to content

Commit

Permalink
Merge pull request #458 from Psychedelic/feat/automatic-testing
Browse files Browse the repository at this point in the history
Feat/automatic testing
  • Loading branch information
tomiir authored May 23, 2022
2 parents d52113d + fff4647 commit 477dcff
Show file tree
Hide file tree
Showing 12 changed files with 1,887 additions and 1,607 deletions.
56 changes: 6 additions & 50 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,10 @@
const JsConfigPathsMapper = require('jsconfig-paths-jest-mapper');
const regeneratorRuntime = require("regenerator-runtime");
const chrome = require('sinon-chrome/extensions');
const firefox = require('selenium-webdriver/firefox');
const browser = require('sinon-chrome/webextensions');
const webdriver = require('selenium-webdriver');
const { Builder, By, Key, Capabilities, until } = require('selenium-webdriver');
const { Options: ChromeOptions } = require('selenium-webdriver/chrome');
const { Options: FirefoxOptions } = require('selenium-webdriver/firefox');
const path = require('path');

const instantiateSeleniumDriver = async () => {
const seleniumChrome = webdriver.Capabilities.chrome();
const seleniumFirefox = webdriver.Capabilities.firefox();

const chromeOptions = new ChromeOptions();
const firefoxOptions = new FirefoxOptions();

chromeOptions.addArguments("--load-extension=" + path.resolve(__dirname, 'extension', 'chrome'));
firefoxOptions.setPreference("xpinstall.signatures.required", false);

const chromeDriver = new webdriver.Builder()
.withCapabilities(seleniumChrome)
.setChromeOptions(chromeOptions)
.build();

const firefoxDriver = new webdriver.Builder()
.forBrowser('firefox')
.setFirefoxOptions(firefoxOptions)
.build();

//await firefoxDriver.installAddon(path.resolve(__dirname, 'extension', 'firefox.xpi'));

return [chromeDriver, firefoxDriver];
};

module.exports = {
moduleFileExtensions: ["js", "jsx"],
preset: "jest-puppeteer",
setupFilesAfterEnv: ['<rootDir>/tests/setup/jestSetup.js'],
transform: {
"^.+\\.jsx?$": "<rootDir>/tests/__setup__/jest.transform.js",
"\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "<rootDir>/tests/__setup__/assetsTransformer.js",
"\\.(css|less)$": "<rootDir>/tests/__setup__/assetsTransformer.js"
},
globals: {
chrome,
browser,
regeneratorRuntime,
getDrivers: instantiateSeleniumDriver,
closeDriver: (drivers) => {
drivers.forEach(driver => driver.quit());
},
"node_modules/variables/.js": "babel-jest",
},
moduleNameMapper: new JsConfigPathsMapper({ configFileName: "./jsconfig.json" }),
transformIgnorePatterns: [
"node_modules/(?!variables/.*)"
]
};
25 changes: 7 additions & 18 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,9 @@
"lint": "eslint ./source --ext .js,.jsx",
"lint:fix": "eslint ./source --ext .js,.jsx --fix",
"prepare": "husky install",
"test": "jest",
"test:sinon": "jest ./tests/sinon",
"test:selenium": "jest ./tests/selenium",
"pretest:selenium": "cross-env SELENIUM_BROWSER=chrome yarn && cross-env SELENIUM_BROWSER=firefox yarn",
"test:watch": "jest --watch"
"test:watch": "jest --watch",
"pretest": "yarn build:inpage && cross-env NODE_ENV=test cross-env TARGET_BROWSER=chrome webpack",
"test": "jest --runInBand"
},
"dependencies": {
"@babel/plugin-proposal-optional-chaining": "^7.16.0",
Expand All @@ -52,6 +50,7 @@
"from-exponential": "^1.1.1",
"i18next": "^20.2.1",
"moment": "^2.29.1",
"puppeteer": "^13.6.0",
"qrcode": "^1.4.4",
"query-string": "^7.0.0",
"random-color": "^1.0.1",
Expand Down Expand Up @@ -79,14 +78,13 @@
"@babel/preset-react": "^7.14.5",
"autoprefixer": "^10.0.2",
"babel-eslint": "^10.1.0",
"babel-jest": "^27.0.6",
"babel-jest": "^28.0.3",
"babel-loader": "^8.2.1",
"babel-plugin-transform-class-properties": "^6.24.1",
"babel-plugin-transform-object-rest-spread": "^6.26.0",
"babel-plugin-transform-react-jsx": "^6.24.1",
"babel-preset-env": "^1.7.0",
"babel-preset-react": "^6.24.1",
"chromedriver": "^92.0.1",
"clean-webpack-plugin": "^3.0.0",
"copy-webpack-plugin": "^6.3.1",
"cross-env": "^7.0.2",
Expand All @@ -102,9 +100,10 @@
"html-webpack-plugin": "^4.5.0",
"husky": ">=6",
"identity-obj-proxy": "^3.0.0",
"jest": "^27.0.6",
"jest": "^28.0.3",
"jest-chrome": "^0.7.1",
"jest-environment-jsdom": "^27.0.6",
"jest-puppeteer": "^6.1.0",
"jest-webextension-mock": "^3.7.18",
"jsconfig-paths-jest-mapper": "^1.0.0",
"lint-staged": ">=10",
Expand All @@ -116,8 +115,6 @@
"react-test-renderer": "^17.0.2",
"regenerator-runtime": "^0.13.9",
"resolve-url-loader": "^3.1.2",
"selenium-webdriver": "^4.0.0-beta.4",
"sinon-chrome": "^3.0.1",
"svg-jest": "^1.0.1",
"terser-webpack-plugin": "^4.2.3",
"webpack": "^4.44.2",
Expand All @@ -138,14 +135,6 @@
"*.{js,jsx}": "eslint --cache --fix",
"*.{js,css,md}": "prettier --write"
},
"jest": {
"setupFiles": [
"<rootDir>/tests/__setup__/setup.js"
],
"setupFilesAfterEnv": [
"<rootDir>/jest.config"
]
},
"standard": {
"parser": "@babel/eslint-parser"
}
Expand Down
7 changes: 0 additions & 7 deletions tests/__setup__/assetsTransformer.js

This file was deleted.

14 changes: 0 additions & 14 deletions tests/__setup__/jest.transform.js

This file was deleted.

14 changes: 0 additions & 14 deletions tests/__setup__/test-enviroment.js

This file was deleted.

199 changes: 199 additions & 0 deletions tests/e2e/import.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
describe('Import/Create', () => {
let chromeBrowser;
let page;

const passwordErrorLabel = '#options-root > div > div.MuiContainer-root.MuiContainer-maxWidthSm > div > div.MuiGrid-root.makeStyles-passwordError-4.MuiGrid-item.MuiGrid-grid-xs-12 > p';

const badSeedphrase = 'sadf adsf adfdfasd adfad adfafd sdfsd sdfsdf sdfds sdfd sdf sfsfs sdfadsf';

const getButtonCollection = async (page) => {
return await page.$$('button');
}

const getImportButton = async (page) => {
const buttonCollection = await getButtonCollection(page);
return buttonCollection[0];
};

const getCreateButton = async (page) => {
const buttonCollection = await getButtonCollection(page);
return buttonCollection[1];
};

beforeAll(async () => {
chromeBrowser = await setupChrome();
});

beforeEach(async () => {
page = await chromeBrowser.newPage();
await page.goto(chromeData.optionsUrl);
});

afterEach(async () => {
await page.close();
});

afterAll(async () => {
await chromeBrowser.close();
});

describe('Import', () => {
beforeEach(async () => {
const importButton = await utils.getButtonWithIndex(page, 0);
await importButton.click();
});

test('Fails on incorrect seedphrase', async () => {
const importInputElement = await utils.getElement(page, 'textarea');
await importInputElement.type(badSeedphrase);

const submitImport = await utils.getButtonWithIndex(page, 0);
await submitImport.click();

const updatedImportBtn = await utils.getButtonWithIndex(page, 0);
const importButtonClassName = await (await updatedImportBtn.getProperty('className')).jsonValue();
const importButtonClassNameArray = importButtonClassName.split(' ');

expect(importButtonClassNameArray).toEqual(expect.arrayContaining(['Mui-disabled']));
});

test('Fails on missmatched passwords', async () => {
const importInputElement = await utils.getElement(page, 'textarea');
await importInputElement.type(secrets.seedphrase);

const submitImport = await utils.getButtonWithIndex(page, 0);
await submitImport.click();

await utils.waitForRender(500);
const [passwordInput, confirmPasswordInput] = await utils.getInputs(page);
await passwordInput.type('TestPassword123');
await confirmPasswordInput.type('MissMatchedPassword');

const submitPassword = await utils.getButtonWithIndex(page, 0);
await submitPassword.click();

await page.waitForSelector(passwordErrorLabel);
const labelErrorElement = await page.$(passwordErrorLabel)
const value = await page.evaluate(el => el.textContent, labelErrorElement);

expect(value).toBe('The passwords gotta match, smh!');
});

test('Fails on password shorter than 12 characters', async () => {
const importInputElement = await utils.getElement(page, 'textarea');
await importInputElement.type(secrets.seedphrase);

const submitImport = await utils.getButtonWithIndex(page, 0);
await submitImport.click();

const [passwordInput, confirmPasswordInput] = await utils.getInputs(page);
await passwordInput.type('123');
await confirmPasswordInput.type('123');

const submitPassword = await utils.getButtonWithIndex(page, 0);
await submitPassword.click();

await page.waitForSelector(passwordErrorLabel);
const labelErrorElement = await page.$(passwordErrorLabel)
const value = await page.evaluate(el => el.textContent, labelErrorElement);

expect(value).toBe('The minimum is 12 characters, smh!');
});

test('Correctly imports', async () => {
const importInputElement = await utils.getElement(page, 'textarea');
await importInputElement.type(secrets.seedphrase);

const submitImport = await utils.getButtonWithIndex(page, 0);
await submitImport.click();

const [passwordInput, confirmPasswordInput] = await utils.getInputs(page, true);
await passwordInput.type(secrets.password);
await confirmPasswordInput.type(secrets.password);

const submitPassword = await utils.getButtonWithIndex(page, 0);
await submitPassword.click();

await page.goto(chromeData.popupUrl);

const popupPasswordInput = await utils.getInputWithIndex(page, 0, true);
await popupPasswordInput.type(secrets.password);

const unlockPlugButton = await utils.getButtonWithIndex(page, 0);
await unlockPlugButton.click();

const [plugBanner] = await utils.getXPathElements(page, 'span', 'Alpha Release', true);
const value = await page.evaluate(el => el.textContent, plugBanner);

expect(value).toMatch(/Plug/i);
});
});

describe('Create', () => {
beforeEach(async () => {
const createButton = await utils.getButtonWithIndex(page, 1);
await createButton.click();
});

test('Fails on missmatched passwords', async () => {
const [passwordInput, confirmPasswordInput] = await utils.getInputs(page);
await passwordInput.type('TestPassword123');
await confirmPasswordInput.type('MissMatchedPassword');

const submitPasswordButton = await utils.getButtonWithIndex(page, 0);
await submitPasswordButton.click();

await page.waitForSelector(passwordErrorLabel);
const labelErrorElement = await page.$(passwordErrorLabel)
const value = await page.evaluate(el => el.textContent, labelErrorElement);

expect(value).toBe('The passwords gotta match, smh!');
});

test('Fails on password shorter than 12 characters', async () => {
const [passwordInput, confirmPasswordInput] = await utils.getInputs(page);
await passwordInput.type('123');
await confirmPasswordInput.type('123');

const submitPasswordButton = await utils.getButtonWithIndex(page, 0);
await submitPasswordButton.click();

await page.waitForSelector(passwordErrorLabel);
const labelErrorElement = await page.$(passwordErrorLabel)
const value = await page.evaluate(el => el.textContent, labelErrorElement);

expect(value).toBe('The minimum is 12 characters, smh!');
});

test('Correctly creates', async () => {
const [passwordInput, confirmPasswordInput] = await utils.getInputs(page);
await passwordInput.type(secrets.password);
await confirmPasswordInput.type(secrets.password);

const submitPasswordButton = await utils.getButtonWithIndex(page, 0);
await submitPasswordButton.click();

const [revealSeedphraseElement] = await utils.getXPathElements(page, 'span', 'Reveal Secret Recovery Phrase', true);
await revealSeedphraseElement.click();

const confirmSeedphraseElement = await utils.getInputWithIndex(page, 0);
await confirmSeedphraseElement.click();

const continueBtn = await utils.getButtonWithIndex(page, 0);
await continueBtn.click();

await page.goto(chromeData.popupUrl);

const popupPasswordInput = await utils.getInputWithIndex(page, 0, true);
await popupPasswordInput.type(secrets.password);

const unlockPlugButton = await utils.getButtonWithIndex(page, 0);
await unlockPlugButton.click();

const [plugBanner] = await utils.getXPathElements(page, 'span', 'Alpha Release', true);
const value = await page.evaluate(el => el.textContent, plugBanner);

expect(value).toMatch(/Plug/i);
});
});
});
Loading

0 comments on commit 477dcff

Please sign in to comment.