Skip to content

Commit

Permalink
feat: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucián Blažek committed May 10, 2024
1 parent 8fbfe9c commit b050462
Show file tree
Hide file tree
Showing 7 changed files with 460 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Release NPM package
name: Main flow
#https://yarnpkg.com/configuration/yarnrc#npmAuthToken
#https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages
#https://github.com/actions/setup-node?tab=readme-ov-file
Expand All @@ -8,22 +8,33 @@ name: Release NPM package
# release:
# types: [created]
on:
workflow_dispatch:
push:
release:
types: [created]
workflow_dispatch:

jobs:
# test:
# runs-on: ubuntu-latest
# steps:
# - uses: actions/checkout@v4
# - uses: actions/setup-node@v4
# with:
# node-version: 22
# - run: npm install
# - run: npm test
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
name: Checkout source code
- uses: actions/setup-node@v4
name: Prepare Node Environment
with:
node-version: 22
cache: yarn
scope: clownmeister
- name: Build and test
run: |
corepack enable
yarn install --immutable
yarn build
yarn test
publish-package:
# needs: test
if: github.event_name == 'release' || github.event_name == 'workflow_dispatch'
needs: test
runs-on: ubuntu-latest
permissions:
packages: write
Expand Down
10 changes: 7 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@clownmeister/czech-holidays",
"type": "module",
"version": "1.0.0-b9",
"version": "1.0.0-b10",
"description": "Provides Czech holidays with no historical data. Supports local storage caching.",
"repository": {
"url": "git+https://github.com/clownmeister/czech-holidays.git"
Expand All @@ -24,16 +24,20 @@
"lint": "eslint src",
"lint:fix": "eslint src --ext .ts,.tsx --fix",
"format": "prettier --check ./src",
"format:fix": "prettier --write ./src"
"format:fix": "prettier --write ./src",
"test": "vitest --run",
"test:watch": "vitest"
},
"devDependencies": {
"@types/jsdom": "^21.1.6",
"@types/node": "^20.12.7",
"autoprefixer": "^10.4.19",
"jsdom": "^24.0.0",
"postcss": "^8.4.38",
"prettier": "^3.2.5",
"typescript": "^5.4.5",
"vite": "^5.2.10",
"vite-plugin-dts": "^3.9.0",
"vitest": "^1.5.3"
"vitest": "^1.6.0"
}
}
88 changes: 88 additions & 0 deletions tests/components/CzechHoliday.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
import CzechHolidays, { HolidaySupportedLocales } from '@app/components/CzechHolidays.ts';
import * as DataProvider from '@app/components/DataProvider.ts';

//TODO: finish this is not nice
const holiday = {
day: 1,
month: 1,
name: {
cs: 'Nový rok',
en: 'New Year\'s Day'
},
description: {
cs: '',
en: ''
},
shopRestriction: 1
};

vi.mock('@app/components/DataProvider.ts', () => ({
getEasterHolidays: vi.fn(() => []),
getFixedHolidays: vi.fn(() => [
{
day: 1,
month: 1,
name: {
cs: 'Nový rok',
en: 'New Year\'s Day'
},
description: {
cs: '',
en: ''
},
shopRestriction: 1
}
])
}));

describe('CzechHolidays', () => {
beforeEach(() => {
localStorage.clear();
vi.resetAllMocks();
});

it('should get holidays for the current year if not in local storage and store them', () => {
const year = new Date().getFullYear();
vi.mocked(DataProvider.getFixedHolidays).mockReturnValue([]);
vi.mocked(DataProvider.getEasterHolidays).mockReturnValue([]);

const holidays = CzechHolidays.getHolidaysForYear(year);
expect(holidays).toHaveLength(1);
// expect(localStorage.setItem).toHaveBeenCalled();
});

it('should determine if a specific date is a holiday', () => {
const date = new Date(new Date().getFullYear(), 0, 1);
const holidays = [holiday];
localStorage.setItem(`holidays-${date.getFullYear()}`, JSON.stringify(holidays));

expect(CzechHolidays.isHoliday(date)).toBe(true);
});

it('should return the holiday name for a given date and locale', () => {
const date = new Date(new Date().getFullYear(), 0, 1);
const holidays = [holiday];
localStorage.setItem(`holidays-${date.getFullYear()}`, JSON.stringify(holidays));

expect(CzechHolidays.getHolidayName(date, HolidaySupportedLocales.English)).toBe('New Year\'s Day');
});

it('should return null for non-holiday dates when checking holiday name', () => {
const date = new Date(new Date().getFullYear(), 6, 10);
expect(CzechHolidays.getHolidayName(date, HolidaySupportedLocales.English)).toBe(null);
});

it('should retrieve the holiday object for a given date', () => {
const date = new Date(new Date().getFullYear(), 0, 1);
const holidays = [holiday];
localStorage.setItem(`holidays-${date.getFullYear()}`, JSON.stringify(holidays));

expect(CzechHolidays.getHoliday(date)).toEqual(holidays[0]);
});

it('should return null when trying to retrieve a holiday object for a non-holiday date', () => {
const date = new Date(new Date().getFullYear(), 6, 10);
expect(CzechHolidays.getHoliday(date)).toBe(null);
});
});
12 changes: 8 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"target": "ES2020",
"baseUrl": "./",
"outDir": "./dist",
"rootDir": "./src",
"paths": {
"@app/*": ["src/*"]
},
Expand All @@ -26,9 +25,14 @@
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,

"types": ["vite/client"]
"types": [
"vite/client",
"vitest/globals"
]
},
"include": ["src"],
"include": [
"src",
"tests"
]
}

6 changes: 2 additions & 4 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { defineConfig } from 'vite';
import dts from 'vite-plugin-dts';
import autoprefixer from 'autoprefixer';

export default defineConfig(({ mode }) => {
return {
export default defineConfig({
resolve: {
alias: {
'@app': resolve(__dirname, 'src')
Expand All @@ -14,7 +13,7 @@ export default defineConfig(({ mode }) => {
lib: {
entry: resolve(__dirname, 'src/index.ts'),
fileName: 'czech-holidays',
formats: ['esm']
formats: ['es']
},
sourcemap: true,
minify: true
Expand All @@ -33,6 +32,5 @@ export default defineConfig(({ mode }) => {
port: 5176,
open: true
}
};
});

10 changes: 10 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { mergeConfig } from 'vite';
import commonConfig from './vite.config';

export default mergeConfig(commonConfig, {
test: {
globals: true,
environment: 'jsdom',
include: ['tests/test.ts', 'tests/**/*.test.ts']
}
});
Loading

0 comments on commit b050462

Please sign in to comment.