Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jest test suite for js/macros.js #4359

Merged
merged 2 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions js/__tests__/macros.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const blockIsMacro = require("../macros");
const getMacroExpansion = require("../macros");
global._ = jest.fn((str) => str);

describe('blockIsMacro', () => {
let mockActivity;

beforeEach(() => {
mockActivity = { blocks: { protoBlockDict: Object.create(null) } };
});

test('should return true if block is in BLOCKISMACRO list', () => {
expect(Boolean(blockIsMacro(mockActivity, 'actionhelp'))).toBe(true);
});

test('should return false if block is neither in protoBlockDict nor BLOCKISMACRO', () => {
expect(Boolean(blockIsMacro(mockActivity, 'nonExistentBlock'))).toBe(false);
});
});

describe('getMacroExpansion', () => {
let mockActivity;

beforeEach(() => {
mockActivity = { blocks: { protoBlockDict: {} } };
});

test('should return macro expansion from protoBlockDict if macroFunc exists', () => {
const mockFunc = jest.fn(() => [['mockedExpansion']]);
mockActivity.blocks.protoBlockDict['customMacro'] = { macroFunc: mockFunc };
expect(getMacroExpansion(mockActivity, 'customMacro', 10, 20)).toEqual([['mockedExpansion']]);
expect(mockFunc).toHaveBeenCalledWith(10, 20);
});

test('should return predefined macro expansion for known blocks', () => {
const expansion = getMacroExpansion(mockActivity, 'actionhelp', 10, 20);
expect(Array.isArray(expansion)).toBe(true);
expect(expansion.length).toBeGreaterThan(0);
});

test('should return null for unknown macros', () => {
expect(getMacroExpansion(mockActivity, 'unknownMacro', 10, 20)).toBeNull();
});
});
4 changes: 4 additions & 0 deletions js/macros.js
Original file line number Diff line number Diff line change
Expand Up @@ -1757,3 +1757,7 @@ const getMacroExpansion = (activity, blkname, x, y) => {
return null;
}
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = blockIsMacro;
module.exports = getMacroExpansion;
}