Skip to content

Commit f1734da

Browse files
committed
Add unit tests
1 parent 4ab1794 commit f1734da

10 files changed

+5079
-1930
lines changed

jest.config.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = {
2+
preset: 'ts-jest',
3+
testEnvironment: 'node',
4+
roots: ['<rootDir>/src', '<rootDir>/test'],
5+
testMatch: ['**/*.test.ts'],
6+
moduleFileExtensions: ['ts', 'js', 'json', 'node'],
7+
moduleNameMapper: {
8+
'^src/(.*)$': '<rootDir>/src/$1',
9+
},
10+
};

package-lock.json

+4,937-1,920
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+16-8
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,31 @@
77
"scripts": {
88
"start": "webpack serve --mode development",
99
"build": "webpack --mode production",
10-
"test": "echo \"Error: no test specified\" && exit 1"
10+
"test": "jest"
1111
},
12-
"keywords": ["homie", "lit-html", "typescript", "iot"],
12+
"keywords": [
13+
"homie",
14+
"lit-html",
15+
"typescript",
16+
"iot"
17+
],
1318
"author": "",
1419
"license": "ISC",
1520
"devDependencies": {
21+
"@types/aframe": "^1.2.2",
22+
"@types/jest": "^29.5.13",
23+
"@types/node": "^16.11.12",
24+
"html-webpack-plugin": "^5.5.0",
25+
"jest": "^29.7.0",
26+
"ts-jest": "^29.2.5",
27+
"ts-loader": "^9.2.6",
1628
"typescript": "^4.5.4",
1729
"webpack": "^5.65.0",
1830
"webpack-cli": "^4.9.1",
19-
"webpack-dev-server": "^4.7.2",
20-
"ts-loader": "^9.2.6",
21-
"html-webpack-plugin": "^5.5.0",
22-
"@types/node": "^16.11.12",
23-
"@types/aframe": "^1.2.2"
31+
"webpack-dev-server": "^4.7.2"
2432
},
2533
"dependencies": {
2634
"aframe": "^1.3.0",
2735
"lit": "^2.6.1"
2836
}
29-
}
37+
}

test/CoralBranch.test.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { CoralBranch } from './src/CoralBranch';
2+
3+
describe('CoralBranch', () => {
4+
let coralBranch: CoralBranch;
5+
6+
beforeEach(() => {
7+
coralBranch = new CoralBranch('testBranch');
8+
});
9+
10+
test('should be created with correct name', () => {
11+
expect(coralBranch.name).toBe('testBranch');
12+
});
13+
14+
test('should have color property', () => {
15+
const colorProperty = coralBranch.getProperty('color');
16+
expect(colorProperty).toBeDefined();
17+
expect(colorProperty?.value).toBe('pink');
18+
});
19+
20+
test('should have length property', () => {
21+
const lengthProperty = coralBranch.getProperty('length');
22+
expect(lengthProperty).toBeDefined();
23+
expect(lengthProperty?.value).toBe(10);
24+
});
25+
});

test/CoralReef.test.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { CoralReef } from './src/CoralReef';
2+
import { CoralBranch } from './src/CoralBranch';
3+
4+
describe('CoralReef', () => {
5+
let coralReef: CoralReef;
6+
7+
beforeEach(() => {
8+
coralReef = new CoralReef('testReef');
9+
});
10+
11+
test('should be created with correct name', () => {
12+
expect(coralReef.name).toBe('testReef');
13+
});
14+
15+
test('should have two coral branches', () => {
16+
const branch1 = coralReef.getNode('branch1');
17+
const branch2 = coralReef.getNode('branch2');
18+
expect(branch1).toBeInstanceOf(CoralBranch);
19+
expect(branch2).toBeInstanceOf(CoralBranch);
20+
});
21+
22+
test('should have correct branch names', () => {
23+
const branch1 = coralReef.getNode('branch1');
24+
const branch2 = coralReef.getNode('branch2');
25+
expect(branch1?.name).toBe('branch1');
26+
expect(branch2?.name).toBe('branch2');
27+
});
28+
});

test/PlayerAvatar.test.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { PlayerAvatar } from './src/PlayerAvatar';
2+
3+
describe('PlayerAvatar', () => {
4+
let playerAvatar: PlayerAvatar;
5+
6+
beforeEach(() => {
7+
playerAvatar = new PlayerAvatar('testPlayer');
8+
});
9+
10+
test('should be created with correct name', () => {
11+
expect(playerAvatar.name).toBe('testPlayer');
12+
});
13+
14+
test('should have position property', () => {
15+
const positionProperty = playerAvatar.getProperty('position');
16+
expect(positionProperty).toBeDefined();
17+
expect(positionProperty?.value).toEqual({ x: 0, y: 0, z: 0 });
18+
});
19+
20+
test('should have rotation property', () => {
21+
const rotationProperty = playerAvatar.getProperty('rotation');
22+
expect(rotationProperty).toBeDefined();
23+
expect(rotationProperty?.value).toEqual({ x: 0, y: 0, z: 0 });
24+
});
25+
});

test/src/CoralBranch.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { HomieNode } from '../../src/HomieNode';
2+
import { HomieProperty } from '../../src/HomieProperty';
3+
4+
export class CoralBranch extends HomieNode {
5+
constructor(name: string) {
6+
super(name);
7+
this.addProperty(new HomieProperty('color', 'pink'));
8+
this.addProperty(new HomieProperty('length', 10));
9+
}
10+
11+
// Add coral branch specific methods here
12+
}

test/src/CoralReef.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { HomieDevice } from '../../src/HomieDevice';
2+
import { CoralBranch } from './CoralBranch';
3+
4+
export class CoralReef extends HomieDevice {
5+
constructor(name: string) {
6+
super(name);
7+
this.addNode(new CoralBranch('branch1'));
8+
this.addNode(new CoralBranch('branch2'));
9+
}
10+
11+
// Add coral reef specific methods here
12+
}

test/src/PlayerAvatar.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { HomieNode } from '../../src/HomieNode';
2+
import { HomieProperty } from '../../src/HomieProperty';
3+
4+
export class PlayerAvatar extends HomieNode {
5+
constructor(name: string) {
6+
super(name);
7+
this.addProperty(new HomieProperty('position', { x: 0, y: 0, z: 0 }));
8+
this.addProperty(new HomieProperty('rotation', { x: 0, y: 0, z: 0 }));
9+
}
10+
11+
// Add player avatar specific methods here
12+
}

tsconfig.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
"allowSyntheticDefaultImports": true,
2020
"resolveJsonModule": true,
2121
"skipLibCheck": true,
22-
"types": ["aframe"]
22+
"types": ["aframe", "jest"]
2323
},
24-
"include": ["src/**/*"],
24+
"include": ["src/**/*", "test/**/*"],
2525
"exclude": ["node_modules", "dist"]
2626
}

0 commit comments

Comments
 (0)