diff --git a/src/entities/BlockNode/BlockNode.spec.ts b/src/entities/BlockNode/BlockNode.spec.ts index 18d0ba4d..fcfd5cf9 100644 --- a/src/entities/BlockNode/BlockNode.spec.ts +++ b/src/entities/BlockNode/BlockNode.spec.ts @@ -135,4 +135,44 @@ describe('BlockNode', () => { }); }); }); + + describe('.updateTuneData()', () => { + beforeEach(() => { + jest.mock('../BlockTune', () => ({ + BlockTune: jest.fn().mockImplementation(() => ({}) as BlockTune), + update: jest.fn(), + })); + }); + + afterEach(() => { + jest.clearAllMocks(); + }); + + it('should call .update() method of the BlockTune', () => { + const blockTuneName = createBlockTuneName('align'); + + const blockTune = new BlockTune({} as BlockTuneConstructorParameters); + + const blockNode = new BlockNode({ + name: createBlockNodeName('paragraph'), + data: {}, + parent: {} as EditorDocument, + tunes: { + [blockTuneName]: blockTune, + }, + }); + + const dataKey = 'align'; + const dataValue = 'left'; + const data = { + [dataKey]: dataValue, + }; + + const spy = jest.spyOn(blockTune, 'update'); + + blockNode.updateTuneData(blockTuneName, data); + + expect(spy).toHaveBeenCalledWith(dataKey, dataValue); + }); + }); }); diff --git a/src/entities/BlockNode/index.ts b/src/entities/BlockNode/index.ts index e67fc662..1b8332c4 100644 --- a/src/entities/BlockNode/index.ts +++ b/src/entities/BlockNode/index.ts @@ -94,6 +94,18 @@ export class BlockNode { tunes: serializedTunes, }; } + + /** + * Updates data in the BlockTune by the BlockTuneName + * + * @param tuneName - The name of the BlockTune + * @param data - The data to update the BlockTune with + */ + public updateTuneData(tuneName: BlockTuneName, data: Record): void { + Object.entries(data).forEach(([key, value]) => { + this.#tunes[tuneName].update(key, value); + }); + } } export { diff --git a/src/entities/BlockTune/index.ts b/src/entities/BlockTune/index.ts index c4caec09..ad9b1d97 100644 --- a/src/entities/BlockTune/index.ts +++ b/src/entities/BlockTune/index.ts @@ -22,7 +22,7 @@ export class BlockTune { * @param args.name - The name of the tune. * @param args.data - Any additional data associated with the tune. */ - constructor({ name, data }: BlockTuneConstructorParameters) { + constructor({ name, data = {} }: BlockTuneConstructorParameters) { this.#name = name; this.#data = data; }