Skip to content

Commit

Permalink
Add unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtechpavlu committed Apr 8, 2024
1 parent 9c46638 commit 2fabb61
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 1 deletion.
69 changes: 69 additions & 0 deletions .github/workflows/verify.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
name: Verify

on:
push:
branches-ignore:
- master

env:
NODE_VERSION: 20

jobs:
unit-test:
name: Unit Tests
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Node.js ${{ env.NODE_VERSION }}
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
registry-url: 'https://registry.npmjs.org'

- name: Cache dependencies
uses: actions/cache@v4
with:
path: ~/.npm
key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-npm-
- name: Install dependencies
run: npm install

- name: Run Unit tests
run: npm run test:unit

compile:
name: Compile
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- name: Setup Node.js ${{ env.NODE_VERSION }}
uses: actions/setup-node@v4
with:
node-version: ${{ env.NODE_VERSION }}
registry-url: 'https://registry.npmjs.org'

- name: Cache dependencies
uses: actions/cache@v4
with:
path: ~/.npm
key: ${{ runner.os }}-npm-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-npm-
- name: Install dependencies
run: npm install

- name: Dry compilation
run: npm run compile

- name: Dry build
run: npm run build
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"compile": "tsc",
"dev": "ts-node src/index.ts",
"format": "prettier . --write",
"test:unit": "jest --config unit-test.config.js"
"test:unit": "jest --config unit-test.config.js",
"test:integration": "npm run --prefix integration test"
},
"devDependencies": {
"@types/jest": "^29.5.12",
Expand Down
59 changes: 59 additions & 0 deletions tests/data/geographyRegistry.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { addGeography, Country, getGeography, Region } from '../../src';

describe('getGeography function', () => {
it('should return an array of countries for given region', () => {
const region = 'europe';
expect(Array.isArray(getGeography(region))).toBe(true);
});

it('should return an array of countries for given country', () => {
const country = 'uk';
expect(Array.isArray(getGeography(country))).toBe(true);
});

it('should return an array of single item for country', () => {
const country = 'uk';
expect(getGeography(country).length).toBe(1);
});

it('should return an expected country', () => {
const country = 'uk';
expect(getGeography(country)[0]!.name).toBe('United Kingdom');
});

it('should be able to add another country', () => {
const country: Country = {
type: 'country',
name: 'Gondor',
capital: 'Minas Tirith',
abbr: 'gndr'
};

addGeography(country);

expect(() => getGeography('gndr')).not.toThrow();
expect(getGeography('gndr')[0]).not.toBeUndefined();
expect(getGeography('gndr')[0]!.name).toBe('Gondor');
});

it('should be able to add another region', () => {
const countries: Country[] = [
{ type: 'country', name: 'Gondor', capital: 'Minas Tirith', abbr: 'gondor' },
{ type: 'country', name: 'Rohan', capital: 'Edoras', abbr: 'rohan' },
{ type: 'country', name: 'Rivendell', capital: 'Rivendell', abbr: 'rivendell' }
];

const middleEarth: Region = {
type: 'region',
abbr: 'middle-earth-region',
name: 'Middle Earth',
subs: ['gondor', 'rohan', 'rivendell']
};

countries.forEach((c) => addGeography(c));
addGeography(middleEarth);

expect(() => getGeography(middleEarth.abbr)).not.toThrow();
expect(getGeography(middleEarth.abbr).length).toBe(3);
});
});

0 comments on commit 2fabb61

Please sign in to comment.