Skip to content

Commit

Permalink
Cleanup jest stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
bigtimebuddy committed Dec 5, 2022
1 parent 10205a2 commit 53d3c06
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 40 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"build:types": "tsc --declaration --emitDeclarationOnly --skipLibCheck --outDir .types_output",
"postbuild:types": "api-extractor run --local",
"demo": "run-p watch serve",
"lint": "eslint src",
"lint": "eslint src test",
"types": "tsc -noEmit",
"test": "run-s lint types unit-test build",
"unit-test": "jest --silent",
Expand Down
86 changes: 60 additions & 26 deletions test/HTMLTextStyle.test.ts
Original file line number Diff line number Diff line change
@@ -1,106 +1,138 @@
import { HTMLTextStyle } from '../src/HTMLTextStyle';
import { TextStyle } from '@pixi/text';

describe('HTMLTextStyle', () => {
it('should create an instance', () => {
describe('HTMLTextStyle', () =>
{
it('should create an instance', () =>
{
expect(new HTMLTextStyle()).toBeTruthy();
});

describe('constructor', () => {
it('should set properties from constructor', () => {
describe('constructor', () =>
{
it('should set properties from constructor', () =>
{
const style = new HTMLTextStyle({
fontFamily: 'Times',
fontSize: 12,
});

expect(style.fontFamily).toBe('Times');
expect(style.fontSize).toBe(12);
});
});

describe('from', () => {
it('should import from TextStyle', () => {
describe('from', () =>
{
it('should import from TextStyle', () =>
{
expect(HTMLTextStyle.from(new TextStyle())).toBeTruthy();
});
it('should import from TextStyle and disconnect', () => {
it('should import from TextStyle and disconnect', () =>
{
const original = new TextStyle();
const style = HTMLTextStyle.from(original);

original.fontSize = 12;
expect(original.fontSize).toBe(12);
expect(style.fontSize).toBe(HTMLTextStyle.defaultOptions.fontSize);
})
});
});

describe('addOverride', () => {
it('should add override', () => {
describe('addOverride', () =>
{
it('should add override', () =>
{
const style = new HTMLTextStyle();
const id = style.styleID;

style.addOverride('color: red');
expect(style.styleID).toBe(id + 1);
});

it('should add override once', () => {
it('should add override once', () =>
{
const style = new HTMLTextStyle();
const id = style.styleID;

style.addOverride('color: red');
style.addOverride('color: red');
expect(style.styleID).toBe(id + 1);
});

it('should remove override', () => {
it('should remove override', () =>
{
const style = new HTMLTextStyle();
const id = style.styleID;

style.addOverride('color: red');
style.removeOverride('color: red');
expect(style.styleID).toBe(id + 2);
});

it('should remove override once', () => {
it('should remove override once', () =>
{
const style = new HTMLTextStyle();
const id = style.styleID;

style.addOverride('color: red');
style.removeOverride('color: red');
style.removeOverride('color: red');
expect(style.styleID).toBe(id + 2);
});
});

describe('toCSS', () => {
it('should converto CSS', () => {
describe('toCSS', () =>
{
it('should converto CSS', () =>
{
const style = new HTMLTextStyle();

expect(style.toCSS(1)).toMatchSnapshot();
});

it('should insert overrides', () => {
it('should insert overrides', () =>
{
const style = new HTMLTextStyle();

style.addOverride('color: red');
expect(style.toCSS(1)).toMatchSnapshot();
});

it('should respect scale', () => {
it('should respect scale', () =>
{
const style = new HTMLTextStyle({
lineHeight: 50,
wordWrap: true,
wordWrapWidth: 200,
});

expect(style.toCSS(2)).toMatchSnapshot();
});
});

describe('toGlobalCSS', () => {
it('should converto CSS', () => {
describe('toGlobalCSS', () =>
{
it('should converto CSS', () =>
{
const style = new HTMLTextStyle();

expect(style.toGlobalCSS()).toMatchSnapshot();
});

it('should converto CSS', () => {
it('should converto CSS', () =>
{
const style = new HTMLTextStyle();
style.globalCSS = `p { color: red; }`

style.globalCSS = `p { color: red; }`;
expect(style.toGlobalCSS()).toMatchSnapshot();
});
});

describe('loadFont', () => {
it('should load a font', async () => {
describe('loadFont', () =>
{
it('should load a font', async () =>
{
const style = new HTMLTextStyle();
const id = style.styleID;
const url = 'http://localhost:8080/resources/Herborn.ttf';
Expand All @@ -118,7 +150,8 @@ describe('HTMLTextStyle', () => {
expect(style.styleID).toBe(id + 3);
});

it('should allow for family, style, weight overrides', async () => {
it('should allow for family, style, weight overrides', async () =>
{
const style = new HTMLTextStyle();
const url = 'http://localhost:8080/resources/Herborn.ttf';

Expand All @@ -137,7 +170,8 @@ describe('HTMLTextStyle', () => {
style.cleanFonts();
});

it('should load a font with ref-counting', async () => {
it('should load a font with ref-counting', async () =>
{
const style1 = new HTMLTextStyle();
const style2 = new HTMLTextStyle();
const style3 = new HTMLTextStyle();
Expand Down Expand Up @@ -165,4 +199,4 @@ describe('HTMLTextStyle', () => {
expect(Object.keys(HTMLTextStyle.availableFonts).length).toBe(0);
});
});
});
});
16 changes: 6 additions & 10 deletions test/jest-global-setup.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import { spawn } from 'child_process';
import { join } from 'path';

export default async function ()
export default async () =>
{
const httpServerProcess = spawn(
'http-server',
['-c-1', `${join(process.cwd(), './test')}`],
{
// See https://nodejs.org/api/child_process.html#spawning-bat-and-cmd-files-on-windows
shell: process.platform === 'win32',
},
);
const rootPath = join(process.cwd(), './test');
const httpServerProcess = spawn('http-server', ['-c-1', rootPath], {
shell: process.platform === 'win32',
});

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
Expand All @@ -30,4 +26,4 @@ export default async function ()
}
});
});
};
};
4 changes: 2 additions & 2 deletions test/jest-global-teardown.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { ChildProcess } from 'child_process';
import kill from 'tree-kill';

export default async function ()
export default async () =>
{
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
Expand All @@ -17,4 +17,4 @@ export default async function ()
kill(httpServerProcess.pid);
await processClose;
}
};
};
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"esModuleInterop": true
},
"include": [
"src/**/*.ts"
"src/**/*.ts",
"test/*.ts"
]
}

0 comments on commit 53d3c06

Please sign in to comment.