Skip to content

Commit 3eeed25

Browse files
authored
Added test files for js section (#4291)
* Added test files for js section Signed-off-by: Justin Charles <charlesjustin2124@gmail.com> * Changed name of file trashcan.test.js to trash.test.js Signed-off-by: Justin Charles <charlesjustin2124@gmail.com> --------- Signed-off-by: Justin Charles <charlesjustin2124@gmail.com>
1 parent dd46e87 commit 3eeed25

File tree

10 files changed

+514
-0
lines changed

10 files changed

+514
-0
lines changed

js/__tests__/basicblocks.test.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
const { initBasicProtoBlocks, BACKWARDCOMPATIBILIYDICT } = require('../basicblocks');
2+
3+
const mockActivity = {
4+
blocks: {
5+
palettes: {},
6+
protoBlockDict: {
7+
block1: { palette: { add: jest.fn() } },
8+
block2: { palette: { add: jest.fn() } },
9+
blockWithoutPalette: {}
10+
}
11+
},
12+
palettes: {},
13+
};
14+
15+
const setupFunctions = [
16+
'setupRhythmBlockPaletteBlocks', 'setupRhythmBlocks', 'setupMeterBlocks',
17+
'setupPitchBlocks', 'setupIntervalsBlocks', 'setupToneBlocks',
18+
'setupOrnamentBlocks', 'setupVolumeBlocks', 'setupDrumBlocks',
19+
'setupWidgetBlocks', 'setupFlowBlocks', 'setupNumberBlocks',
20+
'setupActionBlocks', 'setupBoxesBlocks', 'setupBooleanBlocks',
21+
'setupHeapBlocks', 'setupDictBlocks', 'setupExtrasBlocks',
22+
'setupProgramBlocks', 'setupGraphicsBlocks', 'setupPenBlocks',
23+
'setupMediaBlocks', 'setupSensorsBlocks', 'setupEnsembleBlocks'
24+
];
25+
26+
setupFunctions.forEach(fnName => {
27+
global[fnName] = jest.fn();
28+
});
29+
30+
describe('initBasicProtoBlocks', () => {
31+
beforeEach(() => {
32+
jest.clearAllMocks();
33+
});
34+
35+
it('should assign palettes to activity.blocks.palettes', () => {
36+
initBasicProtoBlocks(mockActivity);
37+
expect(mockActivity.blocks.palettes).toBe(mockActivity.palettes);
38+
});
39+
40+
it('should call all setup functions with activity', () => {
41+
initBasicProtoBlocks(mockActivity);
42+
setupFunctions.forEach(fnName => {
43+
expect(global[fnName]).toHaveBeenCalledWith(mockActivity);
44+
});
45+
});
46+
47+
it('should add blocks with palettes to their respective palettes', () => {
48+
initBasicProtoBlocks(mockActivity);
49+
50+
expect(mockActivity.blocks.protoBlockDict.block1.palette.add).toHaveBeenCalledWith(
51+
mockActivity.blocks.protoBlockDict.block1
52+
);
53+
expect(mockActivity.blocks.protoBlockDict.block2.palette.add).toHaveBeenCalledWith(
54+
mockActivity.blocks.protoBlockDict.block2
55+
);
56+
expect(mockActivity.blocks.protoBlockDict.blockWithoutPalette.palette).toBeUndefined();
57+
});
58+
});
59+
60+
describe('BACKWARDCOMPATIBILIYDICT', () => {
61+
it('should be defined and not empty', () => {
62+
expect(BACKWARDCOMPATIBILIYDICT).toBeDefined();
63+
expect(Object.keys(BACKWARDCOMPATIBILIYDICT).length).toBeGreaterThan(0);
64+
});
65+
66+
it('should correctly map old block names to new block names', () => {
67+
expect(BACKWARDCOMPATIBILIYDICT.fullscreen).toBe('vspace');
68+
expect(BACKWARDCOMPATIBILIYDICT.seth).toBe('setheading');
69+
expect(BACKWARDCOMPATIBILIYDICT.random2).toBe('random');
70+
});
71+
});

js/__tests__/boundary.test.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
global.base64Encode = jest.fn((str) => str);
2+
global.createjs = {
3+
Container: jest.fn(() => ({
4+
children: [],
5+
addChild: jest.fn(),
6+
removeChild: jest.fn(),
7+
visible: true,
8+
})),
9+
Bitmap: jest.fn(),
10+
};
11+
12+
global.window = {
13+
btoa: jest.fn((str) => Buffer.from(str).toString("base64")),
14+
};
15+
16+
global.BOUNDARY = `
17+
<svg xmlns="http://www.w3.org/2000/svg" width="WIDTH" height="HEIGHT">
18+
<rect x="X" y="Y" width="DX" height="DY" stroke="stroke_color" fill="none" stroke-width="2"/>
19+
</svg>
20+
`;
21+
22+
const Boundary = require('../boundary');
23+
24+
describe('Boundary Class', () => {
25+
let stage;
26+
let boundary;
27+
28+
beforeEach(() => {
29+
stage = {
30+
addChild: jest.fn(),
31+
setChildIndex: jest.fn(),
32+
};
33+
34+
boundary = new Boundary(stage);
35+
});
36+
37+
afterEach(() => {
38+
jest.clearAllMocks();
39+
});
40+
41+
it('should initialize with a container and add it to the stage', () => {
42+
expect(stage.addChild).toHaveBeenCalledWith(boundary._container);
43+
expect(stage.setChildIndex).toHaveBeenCalledWith(boundary._container, 0);
44+
});
45+
46+
it('should call create and destroy methods when setting scale', () => {
47+
const createSpy = jest.spyOn(boundary, 'create');
48+
const destroySpy = jest.spyOn(boundary, 'destroy');
49+
50+
boundary.setScale(800, 600, 2);
51+
52+
expect(destroySpy).toHaveBeenCalled();
53+
expect(createSpy).toHaveBeenCalledWith(800, 600, 2);
54+
});
55+
56+
it('should correctly determine if a point is off-screen', () => {
57+
boundary.create(800, 600, 2);
58+
59+
expect(boundary.offScreen(50, 50)).toBe(true);
60+
expect(boundary.offScreen(boundary.x + 1, boundary.y + 1)).toBe(false);
61+
expect(boundary.offScreen(boundary.x + boundary.dx + 1, boundary.y + boundary.dy + 1)).toBe(true);
62+
});
63+
64+
it('should hide and show the container', () => {
65+
boundary.hide();
66+
expect(boundary._container.visible).toBe(false);
67+
68+
boundary.show();
69+
expect(boundary._container.visible).toBe(true);
70+
});
71+
72+
it('should destroy the first child in the container', () => {
73+
const childMock = {};
74+
boundary._container.children.push(childMock);
75+
76+
boundary.destroy();
77+
expect(boundary._container.removeChild).toHaveBeenCalledWith(childMock);
78+
});
79+
80+
it('should create a boundary with the correct dimensions and add it to the container', () => {
81+
const mockImage = { onload: null, src: '' };
82+
const imgMock = jest.spyOn(global, 'Image').mockImplementation(() => mockImage);
83+
84+
boundary.create(800, 600, 2);
85+
86+
expect(mockImage.onload).not.toBeNull();
87+
expect(mockImage.src).toContain('data:image/svg+xml;base64,');
88+
imgMock.mockRestore();
89+
});
90+
});

js/__tests__/languagebox.test.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
const LanguageBox = require("../languagebox");
2+
3+
const mockActivity = {
4+
storage: {
5+
languagePreference: "enUS",
6+
kanaPreference: null,
7+
},
8+
textMsg: jest.fn(),
9+
};
10+
11+
Object.defineProperty(global, "localStorage", {
12+
value: {
13+
getItem: jest.fn(),
14+
setItem: jest.fn(),
15+
},
16+
writable: true,
17+
});
18+
19+
delete global.window.location;
20+
global.window.location = {
21+
reload: jest.fn(),
22+
};
23+
24+
document.querySelectorAll = jest.fn(() => []);
25+
26+
global._ = jest.fn((str) => str);
27+
28+
describe("LanguageBox Class", () => {
29+
let languageBox;
30+
31+
beforeEach(() => {
32+
jest.clearAllMocks();
33+
languageBox = new LanguageBox(mockActivity);
34+
});
35+
36+
it("should reload the window when OnClick is called", () => {
37+
languageBox.OnClick();
38+
expect(global.window.location.reload).toHaveBeenCalled();
39+
});
40+
41+
it("should display 'already set' message when the selected language is the same", () => {
42+
localStorage.getItem.mockReturnValue("enUS");
43+
mockActivity.textMsg.mockImplementation();
44+
45+
languageBox._language = "enUS";
46+
languageBox.hide();
47+
48+
expect(mockActivity.textMsg).toHaveBeenCalledWith(
49+
"Music Blocks is already set to this language."
50+
);
51+
});
52+
53+
it("should display the refresh message when a new language is selected", () => {
54+
localStorage.getItem.mockReturnValue("ja");
55+
mockActivity.textMsg.mockImplementation();
56+
57+
languageBox._language = "enUS";
58+
languageBox.hide();
59+
60+
expect(mockActivity.textMsg).toHaveBeenCalledWith(
61+
expect.stringContaining("Refresh your browser to change your language preference.")
62+
);
63+
});
64+
65+
it("should display the correct message when hide is called for 'ja'", () => {
66+
localStorage.getItem.mockReturnValue("enUS");
67+
mockActivity.textMsg.mockImplementation();
68+
69+
languageBox._language = "ja";
70+
languageBox.hide();
71+
72+
expect(mockActivity.textMsg).toHaveBeenCalledWith(
73+
expect.stringContaining("言語を変えるには、ブラウザをこうしんしてください。")
74+
);
75+
});
76+
77+
it("should attach click listeners to language links when hide is called", () => {
78+
const mockLinks = [{ addEventListener: jest.fn() }, { addEventListener: jest.fn() }];
79+
document.querySelectorAll.mockReturnValue(mockLinks);
80+
81+
languageBox.hide();
82+
83+
mockLinks.forEach((link) => {
84+
expect(link.addEventListener).toHaveBeenCalledWith("click", expect.any(Function));
85+
});
86+
});
87+
});

js/__tests__/pastebox.test.js

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
const PasteBox = require("../pastebox");
2+
3+
const mockActivity = {
4+
stage: {
5+
addChild: jest.fn(),
6+
},
7+
refreshCanvas: jest.fn(),
8+
};
9+
10+
const mockDocById = jest.fn();
11+
global.docById = mockDocById;
12+
13+
const mockCreatejs = {
14+
Container: jest.fn(() => ({
15+
addChild: jest.fn(),
16+
getBounds: jest.fn(() => ({
17+
x: 0,
18+
y: 0,
19+
width: 100,
20+
height: 50,
21+
})),
22+
on: jest.fn(),
23+
})),
24+
Shape: jest.fn(() => ({
25+
graphics: {
26+
beginFill: jest.fn().mockReturnThis(),
27+
drawRect: jest.fn().mockReturnThis(),
28+
},
29+
})),
30+
Bitmap: jest.fn(),
31+
};
32+
global.createjs = mockCreatejs;
33+
34+
global.base64Encode = jest.fn((data) => data);
35+
36+
describe("PasteBox Class", () => {
37+
let pasteBox;
38+
39+
beforeEach(() => {
40+
jest.clearAllMocks();
41+
pasteBox = new PasteBox(mockActivity);
42+
});
43+
44+
it("should initialize with the correct default values", () => {
45+
expect(pasteBox.activity).toBe(mockActivity);
46+
expect(pasteBox._container).toBe(null);
47+
expect(pasteBox.save).toBe(null);
48+
expect(pasteBox.close).toBe(null);
49+
expect(pasteBox._scale).toBe(1);
50+
});
51+
52+
it("should hide the container and clear the paste element", () => {
53+
pasteBox._container = { visible: true };
54+
mockDocById.mockReturnValue({
55+
value: "",
56+
style: { visibility: "visible" },
57+
});
58+
59+
pasteBox.hide();
60+
61+
expect(pasteBox._container.visible).toBe(false);
62+
expect(mockActivity.refreshCanvas).toHaveBeenCalled();
63+
expect(mockDocById).toHaveBeenCalledWith("paste");
64+
expect(mockDocById("paste").value).toBe("");
65+
expect(mockDocById("paste").style.visibility).toBe("hidden");
66+
});
67+
68+
it("should create a container and add it to the stage", () => {
69+
pasteBox.createBox(2, 100, 200);
70+
71+
expect(pasteBox._scale).toBe(2);
72+
expect(mockCreatejs.Container).toHaveBeenCalled();
73+
expect(mockActivity.stage.addChild).toHaveBeenCalledWith(pasteBox._container);
74+
expect(pasteBox._container.x).toBe(100);
75+
expect(pasteBox._container.y).toBe(200);
76+
});
77+
78+
it("should make the container visible and show the paste element", () => {
79+
pasteBox._container = { visible: false };
80+
mockDocById.mockReturnValue({ style: { visibility: "hidden" } });
81+
82+
pasteBox.show();
83+
84+
expect(pasteBox._container.visible).toBe(true);
85+
expect(mockActivity.refreshCanvas).toHaveBeenCalled();
86+
expect(mockDocById).toHaveBeenCalledWith("paste");
87+
expect(mockDocById("paste").style.visibility).toBe("visible");
88+
});
89+
90+
it("should return the position of the container", () => {
91+
pasteBox._container = { x: 150, y: 250 };
92+
const position = pasteBox.getPos();
93+
94+
expect(position).toEqual([150, 250]);
95+
});
96+
97+
it("should set up the click handler for the container", () => {
98+
const mockBounds = { x: 0, y: 0, width: 100, height: 50 };
99+
pasteBox._container = {
100+
getBounds: jest.fn().mockReturnValue(mockBounds),
101+
hitArea: null,
102+
on: jest.fn(),
103+
};
104+
105+
pasteBox._loadClearContainerHandler();
106+
107+
expect(mockCreatejs.Shape).toHaveBeenCalled();
108+
expect(pasteBox._container.getBounds).toHaveBeenCalled();
109+
expect(pasteBox._container.on).toHaveBeenCalledWith("click", expect.any(Function));
110+
});
111+
112+
it("should create a bitmap from SVG data and call the callback", () => {
113+
const mockCallback = jest.fn();
114+
const mockImg = { onload: null };
115+
jest.spyOn(global, "Image").mockImplementation(() => mockImg);
116+
117+
pasteBox._makeBoxBitmap("data", "box", mockCallback, null);
118+
119+
mockImg.onload();
120+
121+
expect(global.base64Encode).toHaveBeenCalledWith("data");
122+
expect(mockCreatejs.Bitmap).toHaveBeenCalled();
123+
expect(mockCallback).toHaveBeenCalled();
124+
});
125+
});

0 commit comments

Comments
 (0)