Skip to content

Commit

Permalink
feat(block-node): implement .updateTuneData method
Browse files Browse the repository at this point in the history
  • Loading branch information
ilyamore88 committed Aug 27, 2023
1 parent 1d42db0 commit 3203d6a
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
40 changes: 40 additions & 0 deletions src/entities/BlockNode/BlockNode.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
});
12 changes: 12 additions & 0 deletions src/entities/BlockNode/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>): void {
Object.entries(data).forEach(([key, value]) => {
this.#tunes[tuneName].update(key, value);
});
}
}

export {
Expand Down
2 changes: 1 addition & 1 deletion src/entities/BlockTune/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 3203d6a

Please sign in to comment.