From 2f8aecbb4f77f85634b75be28f70976a6b1203f8 Mon Sep 17 00:00:00 2001 From: Nick Kosarev Date: Sat, 22 Jun 2024 17:40:27 +0200 Subject: [PATCH] chore: some refactoring and new tests (#177) * chore: removed old service * chore: added wallabyjs to container * feat: env in dev container * feat: vitest plugin with cool coverage ui * chore: some more tests, refactoring * fix: date tests --- .devcontainer/devcontainer.env | 0 .devcontainer/devcontainer.json | 8 ++++++-- src/lib/utils/__tests__/date.spec.ts | 18 +++++++++--------- src/lib/utils/__tests__/locale.spec.ts | 10 ++++++++++ src/lib/utils/__tests__/random.spec.ts | 11 ++++++++++- src/lib/utils/locale.ts | 18 ++++++++++++------ .../[lang]/(game)/play/GameEventTimer.svelte | 12 ++++++------ 7 files changed, 53 insertions(+), 24 deletions(-) create mode 100644 .devcontainer/devcontainer.env diff --git a/.devcontainer/devcontainer.env b/.devcontainer/devcontainer.env new file mode 100644 index 00000000..e69de29b diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json index 1f4a956f..a7d08af4 100644 --- a/.devcontainer/devcontainer.json +++ b/.devcontainer/devcontainer.json @@ -1,6 +1,5 @@ { "name": "Chat Game", - "service": "app", "image": "mcr.microsoft.com/devcontainers/typescript-node:20", "workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}", @@ -19,11 +18,16 @@ "vscode": { // Add the IDs of extensions you want installed when the container is created. "extensions": [ - "dbaeumer.vscode-eslint" + "dbaeumer.vscode-eslint", + "svelte.svelte-vscode", + "ms-azuretools.vscode-docker", + "vitest.explorer" ] } }, + "runArgs": ["--env-file", ".devcontainer/devcontainer.env"], + "portsAttributes": { "3000": { "label": "Application", diff --git a/src/lib/utils/__tests__/date.spec.ts b/src/lib/utils/__tests__/date.spec.ts index a5b69265..e802fd83 100644 --- a/src/lib/utils/__tests__/date.spec.ts +++ b/src/lib/utils/__tests__/date.spec.ts @@ -7,13 +7,13 @@ describe('getDatePlusMinutes()', () => { const minutesToAdd = 5 const result = getDatePlusMinutes(minutesToAdd) const expected = new Date(now.getTime() + minutesToAdd * 60 * 1000) - expect(result.getTime()).toBeCloseTo(expected.getTime(), 0) + expect(result.getTime()).toBeCloseTo(expected.getTime(), 2) }) it('handles the edge case of zero minutes', () => { const now = new Date() const result = getDatePlusMinutes(0) - expect(result.getTime()).toBeCloseTo(now.getTime(), 0) + expect(result.getTime()).toBeCloseTo(now.getTime(), 2) }) it('handles the edge case of negative minutes', () => { @@ -21,7 +21,7 @@ describe('getDatePlusMinutes()', () => { const minutesToAdd = -5 const result = getDatePlusMinutes(minutesToAdd) const expected = new Date(now.getTime() + minutesToAdd * 60 * 1000) - expect(result.getTime()).toBeCloseTo(expected.getTime(), 0) + expect(result.getTime()).toBeCloseTo(expected.getTime(), 2) }) }) @@ -31,13 +31,13 @@ describe('getDateMinusMinutes()', () => { const minutesToSubtract = 5 const result = getDateMinusMinutes(minutesToSubtract) const expected = new Date(now.getTime() - minutesToSubtract * 60 * 1000) - expect(result.getTime()).toBeCloseTo(expected.getTime(), 0) + expect(result.getTime()).toBeCloseTo(expected.getTime(), 2) }) it('handles the edge case of zero minutes', () => { const now = new Date() const result = getDateMinusMinutes(0) - expect(result.getTime()).toBeCloseTo(now.getTime(), 0) + expect(result.getTime()).toBeCloseTo(now.getTime(), 2) }) it('handles the edge case of negative minutes', () => { @@ -45,7 +45,7 @@ describe('getDateMinusMinutes()', () => { const minutesToSubtract = -5 const result = getDateMinusMinutes(minutesToSubtract) const expected = new Date(now.getTime() - minutesToSubtract * 60 * 1000) - expect(result.getTime()).toBeCloseTo(expected.getTime(), 0) + expect(result.getTime()).toBeCloseTo(expected.getTime(), 2) }) }) @@ -55,13 +55,13 @@ describe('getDatePlusSeconds()', () => { const secondsToAdd = 5 const result = getDatePlusSeconds(secondsToAdd) const expected = new Date(now.getTime() + secondsToAdd * 1000) - expect(result.getTime()).toBeCloseTo(expected.getTime(), 0) + expect(result.getTime()).toBeCloseTo(expected.getTime(), 2) }) it('handles the edge case of zero seconds', () => { const now = new Date() const result = getDatePlusSeconds(0) - expect(result.getTime()).toBeCloseTo(now.getTime(), 0) + expect(result.getTime()).toBeCloseTo(now.getTime(), 2) }) it('handles the edge case of negative seconds', () => { @@ -69,6 +69,6 @@ describe('getDatePlusSeconds()', () => { const secondsToAdd = -5 const result = getDatePlusSeconds(secondsToAdd) const expected = new Date(now.getTime() + secondsToAdd * 1000) - expect(result.getTime()).toBeCloseTo(expected.getTime(), 0) + expect(result.getTime()).toBeCloseTo(expected.getTime(), 2) }) }) diff --git a/src/lib/utils/__tests__/locale.spec.ts b/src/lib/utils/__tests__/locale.spec.ts index d2719416..67e84c72 100644 --- a/src/lib/utils/__tests__/locale.spec.ts +++ b/src/lib/utils/__tests__/locale.spec.ts @@ -26,4 +26,14 @@ describe('pluralizationRu()', () => { const result = pluralizationRu(-3, ['яблоко', 'яблока', 'яблок']) expect(result).toBe('яблока') }) + + it('handles the edge case of 101 - 110', () => { + const result = pluralizationRu(102, ['яблоко', 'яблока', 'яблок']) + expect(result).toBe('яблока') + }) + + it('handles the edge case of 124', () => { + const result = pluralizationRu(124, ['яблоко', 'яблока', 'яблок']) + expect(result).toBe('яблока') + }) }) diff --git a/src/lib/utils/__tests__/random.spec.ts b/src/lib/utils/__tests__/random.spec.ts index 3138dae6..a66368bd 100644 --- a/src/lib/utils/__tests__/random.spec.ts +++ b/src/lib/utils/__tests__/random.spec.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest' -import { getRandomInRange } from '$lib/utils/random' +import { getMinusOrPlus, getRandomInRange } from '$lib/utils/random' describe('getRandomInRange()', () => { it('returns a number within the range', () => { @@ -19,3 +19,12 @@ describe('getRandomInRange()', () => { expect(result).toBeLessThanOrEqual(-1) }) }) + +describe('getMinusOrPlus()', () => { + it('returns 1 or -1', () => { + const result = getMinusOrPlus() + expect(result).toBeGreaterThanOrEqual(-1) + expect(result).toBeLessThanOrEqual(1) + expect(result).not.toBe(0) + }) +}) diff --git a/src/lib/utils/locale.ts b/src/lib/utils/locale.ts index d07422d5..b07ce357 100644 --- a/src/lib/utils/locale.ts +++ b/src/lib/utils/locale.ts @@ -1,9 +1,15 @@ export function pluralizationRu(int: number, array: [string, string, string]): string { - const i = Math.abs(int) + const n = Math.abs(int) - return array[ - i % 100 > 4 && i % 100 < 20 - ? 2 - : [2, 0, 1, 1, 1, 2][i % 10 < 5 ? i % 10 : 5] - ] + let idx + // @see http://docs.translatehouse.org/projects/localization-guide/en/latest/l10n/pluralforms.html + if (n % 10 === 1 && n % 100 !== 11) { + idx = 0 // one + } else if (n % 10 >= 2 && n % 10 <= 4 && (n % 100 < 10 || n % 100 >= 20)) { + idx = 1 // few + } else { + idx = 2 // many + } + + return array[idx] } diff --git a/src/routes/[lang]/(game)/play/GameEventTimer.svelte b/src/routes/[lang]/(game)/play/GameEventTimer.svelte index 8e97cbcc..f6f0633b 100644 --- a/src/routes/[lang]/(game)/play/GameEventTimer.svelte +++ b/src/routes/[lang]/(game)/play/GameEventTimer.svelte @@ -1,23 +1,23 @@