Skip to content

Commit 37dab70

Browse files
authored
test suite for js/palette.js (#4403)
* creating test for js/palette.js * Exporting modules for test
1 parent f07db0f commit 37dab70

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

js/__tests__/palette.test.js

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
const { Palettes, initPalettes } = require('../palette');
2+
3+
global.LEADING = 10;
4+
global.DEFAULTPALETTE = "default";
5+
global.MULTIPALETTES = ["palette1", "palette2"];
6+
global.PALETTEICONS = { "search": "<svg></svg>", "palette1": "<svg></svg>", "palette2": "<svg></svg>" };
7+
global.MULTIPALETTEICONS = ["palette1", "palette2"];
8+
global.SKIPPALETTES = [];
9+
global.toTitleCase = (str) => str.charAt(0).toUpperCase() + str.slice(1);
10+
global._ = (str) => str;
11+
global.platformColor = {
12+
selectorSelected: "#000",
13+
paletteBackground: "#fff",
14+
strokeColor: "#333",
15+
fillColor: "#666",
16+
paletteLabelBackground: "#ccc",
17+
hoverColor: "#ddd",
18+
paletteText: "#000"
19+
};
20+
21+
describe('Palettes Class', () => {
22+
let mockActivity;
23+
let palettes;
24+
25+
beforeEach(() => {
26+
const paletteMock = {
27+
style: { visibility: 'visible' },
28+
children: [
29+
{
30+
children: [
31+
{},
32+
{
33+
children: [
34+
{},
35+
{
36+
parentNode: { removeChild: jest.fn() },
37+
appendChild: jest.fn(() => ({})),
38+
}
39+
]
40+
}
41+
]
42+
}
43+
]
44+
};
45+
46+
global.docById = jest.fn((id) => {
47+
if (id === "PaletteBody") {
48+
return { parentNode: { removeChild: jest.fn() } };
49+
}
50+
if (id === "palette") {
51+
return {
52+
...paletteMock,
53+
children: [
54+
{ children: [{}, { children: [{}, { removeChild: jest.fn(), appendChild: jest.fn(() => ({})) }] }] }
55+
],
56+
};
57+
}
58+
return { style: {}, appendChild: jest.fn(), removeChild: jest.fn() };
59+
});
60+
61+
mockActivity = {
62+
cellSize: 50,
63+
blocks: { protoBlockDict: {}, makeBlock: jest.fn(() => ({})) },
64+
hideSearchWidget: jest.fn(),
65+
showSearchWidget: jest.fn(),
66+
palettes: {},
67+
};
68+
69+
palettes = new Palettes(mockActivity);
70+
});
71+
72+
test('constructor initializes properties correctly', () => {
73+
expect(palettes.activity).toBe(mockActivity);
74+
expect(palettes.cellSize).toBe(Math.floor(50 * 0.5 + 0.5));
75+
});
76+
77+
test('init method sets halfCellSize', () => {
78+
palettes.init();
79+
expect(palettes.halfCellSize).toBe(Math.floor(palettes.cellSize / 2));
80+
});
81+
82+
test('setSize updates cellSize', () => {
83+
palettes.setSize(100);
84+
expect(palettes.cellSize).toBe(Math.floor(100 * 0.5 + 0.5));
85+
});
86+
87+
test('setMobile updates mobile flag and hides menus', () => {
88+
const spyHideMenus = jest.spyOn(palettes, '_hideMenus');
89+
palettes.setMobile(true);
90+
expect(palettes.mobile).toBe(true);
91+
expect(spyHideMenus).toHaveBeenCalled();
92+
});
93+
94+
test('getProtoNameAndPalette returns correct values', () => {
95+
mockActivity.blocks.protoBlockDict = {
96+
testBlock: { name: 'testBlock', palette: { name: 'testPalette' }, hidden: false }
97+
};
98+
expect(palettes.getProtoNameAndPalette('testBlock')).toEqual(['testBlock', 'testPalette', 'testBlock']);
99+
});
100+
101+
test('hide and show functions modify visibility', () => {
102+
palettes.hide();
103+
console.log('Visibility after hide:', docById('palette').style.visibility);
104+
expect(docById('palette').style.visibility).toBe('hidden');
105+
palettes.show();
106+
console.log('Visibility after show:', docById('palette').style.visibility);
107+
expect(docById('palette').style.visibility).toBe('visible');
108+
});
109+
});
110+
111+
describe('initPalettes function', () => {
112+
let palettes;
113+
114+
beforeEach(() => {
115+
palettes = { add: jest.fn(), init_selectors: jest.fn(), makePalettes: jest.fn(), show: jest.fn() };
116+
global.BUILTINPALETTES = ['default'];
117+
});
118+
119+
test('initPalettes initializes palettes correctly', async () => {
120+
await initPalettes(palettes);
121+
expect(palettes.add).toHaveBeenCalledWith('default');
122+
expect(palettes.init_selectors).toHaveBeenCalled();
123+
expect(palettes.makePalettes).toHaveBeenCalledWith(0);
124+
expect(palettes.show).toHaveBeenCalled();
125+
});
126+
});

js/palette.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,3 +1452,7 @@ const initPalettes = async (palettes) => {
14521452
console.debug("Time to show the palettes.");
14531453
palettes.show();
14541454
};
1455+
1456+
if (typeof module !== "undefined" && module.exports) {
1457+
module.exports = { Palettes, initPalettes };
1458+
}

0 commit comments

Comments
 (0)