diff --git a/js/__tests__/macros.test.js b/js/__tests__/macros.test.js new file mode 100644 index 0000000000..ffa35ab28c --- /dev/null +++ b/js/__tests__/macros.test.js @@ -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(); + }); +}); diff --git a/js/macros.js b/js/macros.js index c46feeec97..21d0cfd3fb 100644 --- a/js/macros.js +++ b/js/macros.js @@ -1757,3 +1757,7 @@ const getMacroExpansion = (activity, blkname, x, y) => { return null; } }; +if (typeof module !== 'undefined' && module.exports) { + module.exports = blockIsMacro; + module.exports = getMacroExpansion; +}