Skip to content

Commit

Permalink
Added test files for js section
Browse files Browse the repository at this point in the history
Signed-off-by: Justin Charles <[email protected]>
  • Loading branch information
justin212407 committed Jan 18, 2025
1 parent 8f4d55b commit f7b2a28
Show file tree
Hide file tree
Showing 10 changed files with 514 additions and 0 deletions.
71 changes: 71 additions & 0 deletions js/__tests__/basicblocks.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
const { initBasicProtoBlocks, BACKWARDCOMPATIBILIYDICT } = require('../basicblocks');

const mockActivity = {
blocks: {
palettes: {},
protoBlockDict: {
block1: { palette: { add: jest.fn() } },
block2: { palette: { add: jest.fn() } },
blockWithoutPalette: {}
}
},
palettes: {},
};

const setupFunctions = [
'setupRhythmBlockPaletteBlocks', 'setupRhythmBlocks', 'setupMeterBlocks',
'setupPitchBlocks', 'setupIntervalsBlocks', 'setupToneBlocks',
'setupOrnamentBlocks', 'setupVolumeBlocks', 'setupDrumBlocks',
'setupWidgetBlocks', 'setupFlowBlocks', 'setupNumberBlocks',
'setupActionBlocks', 'setupBoxesBlocks', 'setupBooleanBlocks',
'setupHeapBlocks', 'setupDictBlocks', 'setupExtrasBlocks',
'setupProgramBlocks', 'setupGraphicsBlocks', 'setupPenBlocks',
'setupMediaBlocks', 'setupSensorsBlocks', 'setupEnsembleBlocks'
];

setupFunctions.forEach(fnName => {
global[fnName] = jest.fn();
});

describe('initBasicProtoBlocks', () => {
beforeEach(() => {
jest.clearAllMocks();
});

it('should assign palettes to activity.blocks.palettes', () => {
initBasicProtoBlocks(mockActivity);
expect(mockActivity.blocks.palettes).toBe(mockActivity.palettes);
});

it('should call all setup functions with activity', () => {
initBasicProtoBlocks(mockActivity);
setupFunctions.forEach(fnName => {
expect(global[fnName]).toHaveBeenCalledWith(mockActivity);
});
});

it('should add blocks with palettes to their respective palettes', () => {
initBasicProtoBlocks(mockActivity);

expect(mockActivity.blocks.protoBlockDict.block1.palette.add).toHaveBeenCalledWith(
mockActivity.blocks.protoBlockDict.block1
);
expect(mockActivity.blocks.protoBlockDict.block2.palette.add).toHaveBeenCalledWith(
mockActivity.blocks.protoBlockDict.block2
);
expect(mockActivity.blocks.protoBlockDict.blockWithoutPalette.palette).toBeUndefined();
});
});

describe('BACKWARDCOMPATIBILIYDICT', () => {
it('should be defined and not empty', () => {
expect(BACKWARDCOMPATIBILIYDICT).toBeDefined();
expect(Object.keys(BACKWARDCOMPATIBILIYDICT).length).toBeGreaterThan(0);
});

it('should correctly map old block names to new block names', () => {
expect(BACKWARDCOMPATIBILIYDICT.fullscreen).toBe('vspace');
expect(BACKWARDCOMPATIBILIYDICT.seth).toBe('setheading');
expect(BACKWARDCOMPATIBILIYDICT.random2).toBe('random');
});
});
90 changes: 90 additions & 0 deletions js/__tests__/boundary.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
global.base64Encode = jest.fn((str) => str);
global.createjs = {
Container: jest.fn(() => ({
children: [],
addChild: jest.fn(),
removeChild: jest.fn(),
visible: true,
})),
Bitmap: jest.fn(),
};

global.window = {
btoa: jest.fn((str) => Buffer.from(str).toString("base64")),
};

global.BOUNDARY = `
<svg xmlns="http://www.w3.org/2000/svg" width="WIDTH" height="HEIGHT">
<rect x="X" y="Y" width="DX" height="DY" stroke="stroke_color" fill="none" stroke-width="2"/>
</svg>
`;

const Boundary = require('../boundary');

describe('Boundary Class', () => {
let stage;
let boundary;

beforeEach(() => {
stage = {
addChild: jest.fn(),
setChildIndex: jest.fn(),
};

boundary = new Boundary(stage);
});

afterEach(() => {
jest.clearAllMocks();
});

it('should initialize with a container and add it to the stage', () => {
expect(stage.addChild).toHaveBeenCalledWith(boundary._container);
expect(stage.setChildIndex).toHaveBeenCalledWith(boundary._container, 0);
});

it('should call create and destroy methods when setting scale', () => {
const createSpy = jest.spyOn(boundary, 'create');
const destroySpy = jest.spyOn(boundary, 'destroy');

boundary.setScale(800, 600, 2);

expect(destroySpy).toHaveBeenCalled();
expect(createSpy).toHaveBeenCalledWith(800, 600, 2);
});

it('should correctly determine if a point is off-screen', () => {
boundary.create(800, 600, 2);

expect(boundary.offScreen(50, 50)).toBe(true);
expect(boundary.offScreen(boundary.x + 1, boundary.y + 1)).toBe(false);
expect(boundary.offScreen(boundary.x + boundary.dx + 1, boundary.y + boundary.dy + 1)).toBe(true);
});

it('should hide and show the container', () => {
boundary.hide();
expect(boundary._container.visible).toBe(false);

boundary.show();
expect(boundary._container.visible).toBe(true);
});

it('should destroy the first child in the container', () => {
const childMock = {};
boundary._container.children.push(childMock);

boundary.destroy();
expect(boundary._container.removeChild).toHaveBeenCalledWith(childMock);
});

it('should create a boundary with the correct dimensions and add it to the container', () => {
const mockImage = { onload: null, src: '' };
const imgMock = jest.spyOn(global, 'Image').mockImplementation(() => mockImage);

boundary.create(800, 600, 2);

expect(mockImage.onload).not.toBeNull();
expect(mockImage.src).toContain('data:image/svg+xml;base64,');
imgMock.mockRestore();
});
});
87 changes: 87 additions & 0 deletions js/__tests__/languagebox.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const LanguageBox = require("../languagebox");

const mockActivity = {
storage: {
languagePreference: "enUS",
kanaPreference: null,
},
textMsg: jest.fn(),
};

Object.defineProperty(global, "localStorage", {
value: {
getItem: jest.fn(),
setItem: jest.fn(),
},
writable: true,
});

delete global.window.location;
global.window.location = {
reload: jest.fn(),
};

document.querySelectorAll = jest.fn(() => []);

global._ = jest.fn((str) => str);

describe("LanguageBox Class", () => {
let languageBox;

beforeEach(() => {
jest.clearAllMocks();
languageBox = new LanguageBox(mockActivity);
});

it("should reload the window when OnClick is called", () => {
languageBox.OnClick();
expect(global.window.location.reload).toHaveBeenCalled();
});

it("should display 'already set' message when the selected language is the same", () => {
localStorage.getItem.mockReturnValue("enUS");
mockActivity.textMsg.mockImplementation();

languageBox._language = "enUS";
languageBox.hide();

expect(mockActivity.textMsg).toHaveBeenCalledWith(
"Music Blocks is already set to this language."
);
});

it("should display the refresh message when a new language is selected", () => {
localStorage.getItem.mockReturnValue("ja");
mockActivity.textMsg.mockImplementation();

languageBox._language = "enUS";
languageBox.hide();

expect(mockActivity.textMsg).toHaveBeenCalledWith(
expect.stringContaining("Refresh your browser to change your language preference.")
);
});

it("should display the correct message when hide is called for 'ja'", () => {
localStorage.getItem.mockReturnValue("enUS");
mockActivity.textMsg.mockImplementation();

languageBox._language = "ja";
languageBox.hide();

expect(mockActivity.textMsg).toHaveBeenCalledWith(
expect.stringContaining("言語を変えるには、ブラウザをこうしんしてください。")
);
});

it("should attach click listeners to language links when hide is called", () => {
const mockLinks = [{ addEventListener: jest.fn() }, { addEventListener: jest.fn() }];
document.querySelectorAll.mockReturnValue(mockLinks);

languageBox.hide();

mockLinks.forEach((link) => {
expect(link.addEventListener).toHaveBeenCalledWith("click", expect.any(Function));
});
});
});
125 changes: 125 additions & 0 deletions js/__tests__/pastebox.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
const PasteBox = require("../pastebox");

const mockActivity = {
stage: {
addChild: jest.fn(),
},
refreshCanvas: jest.fn(),
};

const mockDocById = jest.fn();
global.docById = mockDocById;

const mockCreatejs = {
Container: jest.fn(() => ({
addChild: jest.fn(),
getBounds: jest.fn(() => ({
x: 0,
y: 0,
width: 100,
height: 50,
})),
on: jest.fn(),
})),
Shape: jest.fn(() => ({
graphics: {
beginFill: jest.fn().mockReturnThis(),
drawRect: jest.fn().mockReturnThis(),
},
})),
Bitmap: jest.fn(),
};
global.createjs = mockCreatejs;

global.base64Encode = jest.fn((data) => data);

describe("PasteBox Class", () => {
let pasteBox;

beforeEach(() => {
jest.clearAllMocks();
pasteBox = new PasteBox(mockActivity);
});

it("should initialize with the correct default values", () => {
expect(pasteBox.activity).toBe(mockActivity);
expect(pasteBox._container).toBe(null);
expect(pasteBox.save).toBe(null);
expect(pasteBox.close).toBe(null);
expect(pasteBox._scale).toBe(1);
});

it("should hide the container and clear the paste element", () => {
pasteBox._container = { visible: true };
mockDocById.mockReturnValue({
value: "",
style: { visibility: "visible" },
});

pasteBox.hide();

expect(pasteBox._container.visible).toBe(false);
expect(mockActivity.refreshCanvas).toHaveBeenCalled();
expect(mockDocById).toHaveBeenCalledWith("paste");
expect(mockDocById("paste").value).toBe("");
expect(mockDocById("paste").style.visibility).toBe("hidden");
});

it("should create a container and add it to the stage", () => {
pasteBox.createBox(2, 100, 200);

expect(pasteBox._scale).toBe(2);
expect(mockCreatejs.Container).toHaveBeenCalled();
expect(mockActivity.stage.addChild).toHaveBeenCalledWith(pasteBox._container);
expect(pasteBox._container.x).toBe(100);
expect(pasteBox._container.y).toBe(200);
});

it("should make the container visible and show the paste element", () => {
pasteBox._container = { visible: false };
mockDocById.mockReturnValue({ style: { visibility: "hidden" } });

pasteBox.show();

expect(pasteBox._container.visible).toBe(true);
expect(mockActivity.refreshCanvas).toHaveBeenCalled();
expect(mockDocById).toHaveBeenCalledWith("paste");
expect(mockDocById("paste").style.visibility).toBe("visible");
});

it("should return the position of the container", () => {
pasteBox._container = { x: 150, y: 250 };
const position = pasteBox.getPos();

expect(position).toEqual([150, 250]);
});

it("should set up the click handler for the container", () => {
const mockBounds = { x: 0, y: 0, width: 100, height: 50 };
pasteBox._container = {
getBounds: jest.fn().mockReturnValue(mockBounds),
hitArea: null,
on: jest.fn(),
};

pasteBox._loadClearContainerHandler();

expect(mockCreatejs.Shape).toHaveBeenCalled();
expect(pasteBox._container.getBounds).toHaveBeenCalled();
expect(pasteBox._container.on).toHaveBeenCalledWith("click", expect.any(Function));
});

it("should create a bitmap from SVG data and call the callback", () => {
const mockCallback = jest.fn();
const mockImg = { onload: null };
jest.spyOn(global, "Image").mockImplementation(() => mockImg);

pasteBox._makeBoxBitmap("data", "box", mockCallback, null);

mockImg.onload();

expect(global.base64Encode).toHaveBeenCalledWith("data");
expect(mockCreatejs.Bitmap).toHaveBeenCalled();
expect(mockCallback).toHaveBeenCalled();
});
});
Loading

0 comments on commit f7b2a28

Please sign in to comment.