Skip to content
This repository has been archived by the owner on Jan 6, 2024. It is now read-only.

Commit

Permalink
#22 Update path mapping in jest unit tests
Browse files Browse the repository at this point in the history
- Add initial test for black color factory
- Update color.test.ts with better syntax
- Configure module mapping for jest
- Add new script to package.json
- Add common to TS module path mapping
- Update imports in sketch.ts
  • Loading branch information
blwatkins committed Dec 30, 2023
1 parent 4138919 commit fae63ba
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 69 deletions.
14 changes: 11 additions & 3 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,25 @@
* See the GNU Affero General Public License for more details.
*/

import type { Config } from 'jest';
import type { JestConfigWithTsJest } from 'ts-jest';

const config: Config = {
const config: JestConfigWithTsJest = {
collectCoverage: true,
coverageDirectory: './tests-coverage',
coverageReporters: ['text', 'lcov'],
errorOnDeprecated: true,
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'mjs', 'cjs', 'json', 'node'],
moduleNameMapper: {
'^color$': '<rootDir>/src/common/color',
'^color/factory$': '<rootDir>/src/common/color/factory',
'^p5-lib$': '<rootDir>/src/common/p5',
'^random$': '<rootDir>/src/common/random',
'^range$': '<rootDir>/src/common/range',
'^common$': '<rootDir>/src/common'
},
testEnvironment: 'jsdom',
testRegex: '/tests/.*\\.(test|spec)?\\.(ts|tsx)$',
transform: {'^.+\\.ts?$': 'ts-jest'},
transform: {'^.+\\.(ts|tsx)$': 'ts-jest'},
verbose: true
};

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"scripts": {
"lint-tests": "npx eslint ./tests",

"test": "echo '(1/3): Removing old output'; rm -r ./dist ./zip ./tests-coverage; echo '(2/3): Building project'; rollup --config; echo '(3/3): Starting tests'; npx jest",
"build-and-test": "echo '(1/3): Removing old output'; rm -r ./dist ./zip ./tests-coverage; echo '(2/3): Building project'; rollup --config; echo '(3/3): Starting tests'; npx jest",
"test": "echo '(1/1): Starting tests'; npx jest",

"build": "echo '(1/2): Removing old output'; rm -r ./dist ./zip; echo '(2/2): Building project'; rollup --config",

Expand Down
2 changes: 1 addition & 1 deletion src/sketch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* See the GNU Affero General Public License for more details.
*/

import { P5Lib, Color } from "./common";
import { P5Lib, Color } from "common";

import '../assets/styles/sketch.css';

Expand Down
82 changes: 18 additions & 64 deletions tests/common/color/color.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
* See the GNU Affero General Public License for more details.
*/

import Color from "../../../src/common/color/color";
import PLib from "p5";
import { P5Lib, Color } from "common";

describe('color tests', (): void => {
const p5: PLib = new PLib((): void => { return; });
const p5: P5Lib = new P5Lib((): void => { return; });

test('default color', (): void => {
const defaultColor: Color = new Color(p5);
Expand All @@ -33,86 +32,41 @@ describe('color tests', (): void => {
const r: number = 255;
const g: number = 47;
const b: number = 165;
const c: PLib.Color = p5.color(r, g, b);
const c: P5Lib.Color = p5.color(r, g, b);
const color: Color = new Color(p5, c);
expect(color.red).toBe(r);
expect(color.green).toBe(g);
expect(color.blue).toBe(b);
expect(color.alpha).toBe(255);
});

test('color built with (r,g,b,a) parameter', (): void => {
// TODO
expect(true).toBe(false); // automatic failure
});
test.todo('color built with (r,g,b,a) parameter');

test('color built with (c) parameter', (): void => {
// TODO
expect(true).toBe(false); // automatic failure
});
test.todo('color built with (c) parameter');

test('color built with (c,a) parameter', (): void => {
// TODO
expect(true).toBe(false); // automatic failure
});
test.todo('color built with (c,a) parameter');

test('color set with (r,g,b) color', (): void => {
// TODO
expect(true).toBe(false); // automatic failure
});
test.todo('color set with (r,g,b) color');

test('color set with (r,g,b,a) color', (): void => {
// TODO
expect(true).toBe(false); // automatic failure
});
test.todo('color set with (r,g,b,a) color');

test('color set with (c) parameter', (): void => {
// TODO
expect(true).toBe(false); // automatic failure
});
test.todo('color set with (c) parameter');

test('color set with (c,a) parameter', (): void => {
// TODO
expect(true).toBe(false); // automatic failure
});
test.todo('color set with (c,a) parameter');

test('set red to value > 255', (): void => {
// TODO
expect(true).toBe(false); // automatic failure
});
test.todo('set red to value > 255');

test('set red to value < 0', (): void => {
// TODO
expect(true).toBe(false); // automatic failure
});
test.todo('set red to value < 0');

test('set green to value > 255', (): void => {
// TODO
expect(true).toBe(false); // automatic failure
});
test.todo('set green to value > 255');

test('set green to value < 0', (): void => {
// TODO
expect(true).toBe(false); // automatic failure
});
test.todo('set green to value < 0');

test('set blue to value > 255', (): void => {
// TODO
expect(true).toBe(false); // automatic failure
});
test.todo('set blue to value > 255');

test('set blue to value < 0', (): void => {
// TODO
expect(true).toBe(false); // automatic failure
});
test.todo('set blue to value < 0');

test('set alpha to value > 255', (): void => {
// TODO
expect(true).toBe(false); // automatic failure
});
test.todo('set alpha to value > 255');

test('set alpha to value < 0', (): void => {
// TODO
expect(true).toBe(false); // automatic failure
});
test.todo('set alpha to value < 0');
});
62 changes: 62 additions & 0 deletions tests/common/color/factory/rgb/black-color-factory.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* Copyright (C) 2023 Brittni Watkins.
*
* This file is a part of brittni and the polar bear's Generative Art Project Template,
* which is released under the GNU Affero General Public License, Version 3.0.
* You may not use this file except in compliance with the license.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. See LICENSE or go to
* https://www.gnu.org/licenses/agpl-3.0.en.html for full license details.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
*/

import {P5Lib, BlackColorFactory, Color} from "common";

function checkValueBetween(value: number, min: number, max: number) {
expect(value).toBeGreaterThanOrEqual(min);
expect(value).toBeLessThanOrEqual(max);
}

function generateConsecutiveArray(max: number): number[] {
const array: number[] = [];

for (let i: number = 1; i <= max; i++) {
array.push(i);
}

return array;
}

function repeatTest(count: number, testName: string ,givenTest: () => void): void {
const array: number[] = generateConsecutiveArray(count);
test.each(array)(testName, givenTest);
}

describe('back color factory tests', (): void => {
const p5: P5Lib = new P5Lib((): void => { return; });

test('get random black color', (): void => {
const factory: BlackColorFactory = new BlackColorFactory(p5);
const color:Color = factory.getRandomColor();
checkValueBetween(color.red, 0, 100);
checkValueBetween(color.green, 0, 100);
checkValueBetween(color.blue, 0, 100);
expect(color.alpha).toBe(255);
});

const testRandomBlackColor = (): void => {
const factory: BlackColorFactory = new BlackColorFactory(p5);
const color:Color = factory.getRandomColor();
checkValueBetween(color.red, 0, 100);
checkValueBetween(color.green, 0, 100);
checkValueBetween(color.blue, 0, 100);
expect(color.alpha).toBe(255);
}

repeatTest(10, 'get random black color - test %i', testRandomBlackColor);
});
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"paths": {
"color": ["./src/common/color"],
"color/factory": ["./src/common/color/factory"],
"common": ["./src/common"],
"p5-lib": ["./src/common/p5"],
"random": ["./src/common/random"],
"range": ["./src/common/range"]
Expand Down

0 comments on commit fae63ba

Please sign in to comment.