Skip to content

Commit 1d144c3

Browse files
authored
Merge pull request #24 from KBLLR/test/improve-test-coverage
test: Improve test coverage for PresetManager and dateUtils
2 parents 98534cf + 7bf9883 commit 1d144c3

File tree

6 files changed

+133
-6
lines changed

6 files changed

+133
-6
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"lint": "eslint . --ext .js --fix",
1212
"format": "prettier --write .",
1313
"clean": "rimraf dist",
14-
"test:scene": "vite src/tests",
14+
"test:scene": "vite",
1515
"test": "npm run test:scene",
1616
"test:playwright": "start-server-and-test \"npm run test:scene\" http://localhost:5173 \"node src/tests/playwright.test.cjs\""
1717
},

src/core/PresetManager.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ export class PresetManager {
2626

2727
/**
2828
* @constructor
29+
* @param {Object} [manifest] - An optional manifest object for testing or custom presets.
2930
* @description Initializes the PresetManager with a manifest of available presets.
3031
*/
31-
constructor() {
32+
constructor(manifest) {
3233
this.#cache = new Map();
33-
this.#manifest = {
34+
this.#manifest = manifest || {
3435
[PRESET_REGISTRY.CAMERA]: {
3536
loader: () => import("@presets/cameraPresets"),
3637
exportName: "CAMERA_PRESETS",

src/tests/index.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@
3333
<canvas></canvas>
3434
<!-- Container for displaying the results of the tests. -->
3535
<div id="testResults"></div>
36-
<!-- Script that runs the actual tests. -->
36+
<!-- Scripts that run the actual tests. -->
3737
<script type="module" src="./sceneTest.js"></script>
38+
<script type="module" src="./presetManager.test.js"></script>
3839
</body>
3940
</html>

src/tests/playwright.test.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const path = require('path');
99
page.on('console', msg => console.log(msg.text()));
1010

1111
try {
12-
await page.goto('http://localhost:5173', { waitUntil: 'networkidle' });
12+
await page.goto('http://localhost:5173/src/tests/', { waitUntil: 'networkidle' });
1313

1414
// Wait for the test results to be populated
1515
await page.waitForSelector('#testResults', { timeout: 30000 });

src/tests/presetManager.test.js

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/**
2+
* @file This file contains tests for the PresetManager class.
3+
* @module presetManagerTest
4+
*/
5+
6+
import { PresetManager } from '../core/PresetManager.js';
7+
import { PRESET_REGISTRY } from '../presets/index.js';
8+
import { CAMERA_PRESETS } from '../presets/cameraPresets.js';
9+
10+
class PresetManagerTest {
11+
constructor() {
12+
this.testResults = document.getElementById("testResults");
13+
this.runTests();
14+
}
15+
16+
logResult(message, passed) {
17+
const result = `${passed ? "✅" : "❌"} ${message}`;
18+
if (this.testResults) {
19+
const div = document.createElement("div");
20+
div.innerHTML = message; // Use innerHTML to render h2 tags
21+
div.style.color = passed ? "#4CAF50" : "#f44336";
22+
this.testResults.appendChild(div);
23+
}
24+
}
25+
26+
async runTests() {
27+
this.logResult("<h2>Starting PresetManager Tests...</h2>", true);
28+
29+
await this.testInitialization();
30+
await this.testGetPresetNames();
31+
await this.testLoadPreset();
32+
await this.testCaching();
33+
34+
this.logResult("<h2>PresetManager Tests Complete</h2>", true);
35+
}
36+
37+
async testInitialization() {
38+
const presetManager = new PresetManager();
39+
this.logResult("PresetManager should initialize", presetManager instanceof PresetManager);
40+
}
41+
42+
async testGetPresetNames() {
43+
const presetManager = new PresetManager();
44+
45+
// Test for camera presets
46+
try {
47+
const cameraPresetNames = await presetManager.getPresetNames(PRESET_REGISTRY.CAMERA);
48+
const expectedCameraPresetNames = Object.keys(CAMERA_PRESETS);
49+
this.logResult("getPresetNames should return correct camera preset names", JSON.stringify(cameraPresetNames.sort()) === JSON.stringify(expectedCameraPresetNames.sort()));
50+
} catch (e) {
51+
this.logResult(`getPresetNames for camera presets failed: ${e.message}`, false);
52+
}
53+
54+
// Test for an invalid preset type
55+
try {
56+
await presetManager.getPresetNames('invalid_type');
57+
this.logResult("getPresetNames should throw an error for an invalid type", false);
58+
} catch (e) {
59+
this.logResult("getPresetNames should throw an error for an invalid type", e.message.includes('Unknown preset type'));
60+
}
61+
}
62+
63+
async testLoadPreset() {
64+
const presetManager = new PresetManager();
65+
66+
// Test loading a valid camera preset
67+
try {
68+
const preset = await presetManager.loadPreset(PRESET_REGISTRY.CAMERA, 'DEFAULT');
69+
this.logResult("loadPreset should load a valid camera preset", preset === CAMERA_PRESETS.DEFAULT);
70+
} catch (e) {
71+
this.logResult(`loadPreset for a valid camera preset failed: ${e.message}`, false);
72+
}
73+
74+
// Test loading a non-existent preset
75+
try {
76+
await presetManager.loadPreset(PRESET_REGISTRY.CAMERA, 'NON_EXISTENT');
77+
this.logResult("loadPreset should throw an error for a non-existent preset", false);
78+
} catch (e) {
79+
this.logResult("loadPreset should throw an error for a non-existent preset", e.message.includes('not found for type'));
80+
}
81+
82+
// Test loading from an invalid preset type
83+
try {
84+
await presetManager.loadPreset('invalid_type', 'DEFAULT');
85+
this.logResult("loadPreset should throw an error for an invalid type", false);
86+
} catch (e) {
87+
this.logResult("loadPreset should throw an error for an invalid type", e.message.includes('Unknown preset type'));
88+
}
89+
}
90+
91+
async testCaching() {
92+
let callCount = 0;
93+
const mockManifest = {
94+
[PRESET_REGISTRY.CAMERA]: {
95+
loader: () => {
96+
callCount++;
97+
return Promise.resolve({
98+
CAMERA_PRESETS: {
99+
'DEFAULT': { "test": "data" }
100+
}
101+
});
102+
},
103+
exportName: "CAMERA_PRESETS",
104+
}
105+
};
106+
107+
const presetManager = new PresetManager(mockManifest);
108+
109+
// Load the same preset twice
110+
await presetManager.loadPreset(PRESET_REGISTRY.CAMERA, 'DEFAULT');
111+
await presetManager.loadPreset(PRESET_REGISTRY.CAMERA, 'DEFAULT');
112+
113+
this.logResult("loadPreset should cache presets and only call loader once", callCount === 1);
114+
}
115+
}
116+
117+
window.addEventListener("DOMContentLoaded", () => {
118+
new PresetManagerTest();
119+
});

src/tests/sceneTest.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* @module sceneTest
55
*/
66

7-
import { getFormattedMonth } from "../utils/dateUtils";
7+
import { getFormattedMonth, getCurrentYear } from "../utils/dateUtils";
88
import { SCENE_REQUIREMENTS } from "../presets/sceneRequirements";
99
import { SCENE_CONFIGS } from "../presets/sceneConfigs";
1010
import * as THREE from "three";
@@ -169,6 +169,12 @@ class SceneTest {
169169
const month = getFormattedMonth(testDate);
170170
const passed = month === "Jan";
171171
this.logResult(`getFormattedMonth should return 'Jan' for January`, passed);
172+
173+
// Test that getCurrentYear returns the correct year as a string.
174+
const currentYear = new Date().getFullYear().toString();
175+
const year = getCurrentYear();
176+
const yearTestPassed = year === currentYear;
177+
this.logResult(`getCurrentYear should return the current year (${currentYear})`, yearTestPassed);
172178
}
173179
}
174180

0 commit comments

Comments
 (0)