From 1565da87cf0cb13573de526cc0323a5d4dbb0c5e Mon Sep 17 00:00:00 2001 From: David Twigger Date: Tue, 10 Jan 2023 08:18:45 +0100 Subject: [PATCH 1/3] Implement cypress unit testing --- config/cypress.frontend.config.js | 10 +++++++ package.json | 1 + test/cypress/unit/i18n.spec.js | 44 +++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) create mode 100644 config/cypress.frontend.config.js create mode 100644 test/cypress/unit/i18n.spec.js diff --git a/config/cypress.frontend.config.js b/config/cypress.frontend.config.js new file mode 100644 index 000000000..eecdcb8dd --- /dev/null +++ b/config/cypress.frontend.config.js @@ -0,0 +1,10 @@ +const { defineConfig } = require("cypress"); + +module.exports = defineConfig({ + e2e: { + supportFile: false, + specPattern: [ + "test/cypress/unit/**/*.js" + ], + } +}); diff --git a/package.json b/package.json index ebe305f92..190ef2478 100644 --- a/package.json +++ b/package.json @@ -60,6 +60,7 @@ "start-pr-test": "node extra/checkout-pr.js && npm install && npm run dev", "cy:test": "node test/prepare-test-server.js && node server/server.js --port=3002 --data-dir=./data/test/ --e2e", "cy:run": "npx cypress run --browser chrome --headless --config-file ./config/cypress.config.js", + "cy:run:unit": "npx cypress run --browser chrome --headless --config-file ./config/cypress.frontend.config.js", "cypress-open": "concurrently -k -r \"node test/prepare-test-server.js && node server/server.js --port=3002 --data-dir=./data/test/\" \"cypress open --config-file ./config/cypress.config.js\"", "build-healthcheck-armv7": "cross-env GOOS=linux GOARCH=arm GOARM=7 go build -x -o ./extra/healthcheck-armv7 ./extra/healthcheck.go" }, diff --git a/test/cypress/unit/i18n.spec.js b/test/cypress/unit/i18n.spec.js new file mode 100644 index 000000000..da63d95a5 --- /dev/null +++ b/test/cypress/unit/i18n.spec.js @@ -0,0 +1,44 @@ +import { currentLocale } from "../../../src/i18n"; + +describe("Test i18n.js", () => { + + it("currentLocale()", () => { + const setLanguage = (language) => { + Object.defineProperty(window.navigator, 'language', { + value: language, + writable: true + }); + } + setLanguage('en-EN'); + + expect(currentLocale()).equal("en"); + + setLanguage('zh-HK'); + expect(currentLocale()).equal("zh-HK"); + + // Note that in Safari on iOS prior to 10.2, the country code returned is lowercase: "en-us", "fr-fr" etc. + // https://developer.mozilla.org/en-US/docs/Web/API/Navigator/language + setLanguage('zh-hk'); + expect(currentLocale()).equal("en"); + + setLanguage('en-US'); + expect(currentLocale()).equal("en"); + + setLanguage('ja-ZZ'); + expect(currentLocale()).equal("ja"); + + setLanguage('zz-ZZ'); + expect(currentLocale()).equal("en"); + + setLanguage('zz-ZZ'); + expect(currentLocale()).equal("en"); + + setLanguage('en'); + localStorage.locale = "en"; + expect(currentLocale()).equal("en"); + + localStorage.locale = "zh-HK"; + expect(currentLocale()).equal("zh-HK"); + }); + +}); \ No newline at end of file From 1c05ba09dcf17f898f8ea3b8a8c6ec6112d92933 Mon Sep 17 00:00:00 2001 From: David Twigger Date: Tue, 10 Jan 2023 08:24:15 +0100 Subject: [PATCH 2/3] Add cypress unit tests to workflow --- .github/workflows/auto-test.yml | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/.github/workflows/auto-test.yml b/.github/workflows/auto-test.yml index b49a253c3..c03e9e4de 100644 --- a/.github/workflows/auto-test.yml +++ b/.github/workflows/auto-test.yml @@ -66,3 +66,19 @@ jobs: - run: npm install - run: npm run build - run: npm run cy:test + + frontend-unit-tests: + needs: [ check-linters ] + runs-on: ubuntu-latest + steps: + - run: git config --global core.autocrlf false # Mainly for Windows + - uses: actions/checkout@v3 + + - name: Use Node.js 14 + uses: actions/setup-node@v3 + with: + node-version: 14 + cache: 'npm' + - run: npm install + - run: npm run build + - run: cy:run:unit From 636fc8fcfcdd46b7d60f367b66a23f13dc3e7815 Mon Sep 17 00:00:00 2001 From: David Twigger Date: Tue, 10 Jan 2023 08:43:39 +0100 Subject: [PATCH 3/3] Fix workflow --- .github/workflows/auto-test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/auto-test.yml b/.github/workflows/auto-test.yml index c03e9e4de..3ea96c2ef 100644 --- a/.github/workflows/auto-test.yml +++ b/.github/workflows/auto-test.yml @@ -81,4 +81,4 @@ jobs: cache: 'npm' - run: npm install - run: npm run build - - run: cy:run:unit + - run: npm run cy:run:unit