Skip to content

Commit 0427716

Browse files
authored
jest test suite for js/macros.js (#4359)
* created test file for macros.js * Exporting modules for test
1 parent d415cc0 commit 0427716

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

js/__tests__/macros.test.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const blockIsMacro = require("../macros");
2+
const getMacroExpansion = require("../macros");
3+
global._ = jest.fn((str) => str);
4+
5+
describe('blockIsMacro', () => {
6+
let mockActivity;
7+
8+
beforeEach(() => {
9+
mockActivity = { blocks: { protoBlockDict: Object.create(null) } };
10+
});
11+
12+
test('should return true if block is in BLOCKISMACRO list', () => {
13+
expect(Boolean(blockIsMacro(mockActivity, 'actionhelp'))).toBe(true);
14+
});
15+
16+
test('should return false if block is neither in protoBlockDict nor BLOCKISMACRO', () => {
17+
expect(Boolean(blockIsMacro(mockActivity, 'nonExistentBlock'))).toBe(false);
18+
});
19+
});
20+
21+
describe('getMacroExpansion', () => {
22+
let mockActivity;
23+
24+
beforeEach(() => {
25+
mockActivity = { blocks: { protoBlockDict: {} } };
26+
});
27+
28+
test('should return macro expansion from protoBlockDict if macroFunc exists', () => {
29+
const mockFunc = jest.fn(() => [['mockedExpansion']]);
30+
mockActivity.blocks.protoBlockDict['customMacro'] = { macroFunc: mockFunc };
31+
expect(getMacroExpansion(mockActivity, 'customMacro', 10, 20)).toEqual([['mockedExpansion']]);
32+
expect(mockFunc).toHaveBeenCalledWith(10, 20);
33+
});
34+
35+
test('should return predefined macro expansion for known blocks', () => {
36+
const expansion = getMacroExpansion(mockActivity, 'actionhelp', 10, 20);
37+
expect(Array.isArray(expansion)).toBe(true);
38+
expect(expansion.length).toBeGreaterThan(0);
39+
});
40+
41+
test('should return null for unknown macros', () => {
42+
expect(getMacroExpansion(mockActivity, 'unknownMacro', 10, 20)).toBeNull();
43+
});
44+
});

js/macros.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,3 +1757,7 @@ const getMacroExpansion = (activity, blkname, x, y) => {
17571757
return null;
17581758
}
17591759
};
1760+
if (typeof module !== 'undefined' && module.exports) {
1761+
module.exports = blockIsMacro;
1762+
module.exports = getMacroExpansion;
1763+
}

0 commit comments

Comments
 (0)