operation-service.test.ts 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /**
  2. * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
  3. * SPDX-License-Identifier: MIT
  4. */
  5. import { describe, it, expect, beforeEach, vi } from 'vitest';
  6. import { OperationRegistry, Operation, OperationService } from '../src';
  7. import { createHistoryContainer } from '../__mocks__/history-container.mock';
  8. const fn = vi.fn();
  9. describe('operation-service', () => {
  10. let operationRegistry: OperationRegistry;
  11. let operationService: OperationService;
  12. let container;
  13. beforeEach(() => {
  14. container = createHistoryContainer();
  15. operationService = container.get(OperationService);
  16. operationRegistry = container.get(OperationRegistry);
  17. const operationMeta = {
  18. type: 'test',
  19. inverse: (op: Operation) => op,
  20. description: 'test',
  21. apply: fn,
  22. getLabel: () => 'test1',
  23. };
  24. operationRegistry.registerOperationMeta(operationMeta);
  25. });
  26. it('get operation label should return correct label', () => {
  27. expect(operationService.getOperationLabel({ type: 'test' } as any)).toEqual('test1');
  28. });
  29. it('no apply', () => {
  30. operationService.applyOperation({ type: 'test' } as any, { noApply: true });
  31. expect(fn).toBeCalledTimes(0);
  32. operationService.applyOperation({ type: 'test' } as any, { noApply: false });
  33. expect(fn).toBeCalledTimes(1);
  34. operationService.applyOperation({ type: 'test' } as any);
  35. expect(fn).toBeCalledTimes(2);
  36. });
  37. it('on apply', () => {
  38. const handleApply = vi.fn();
  39. operationService.onApply(handleApply);
  40. operationService.applyOperation({ type: 'test' } as any);
  41. expect(handleApply).toBeCalledTimes(1);
  42. operationService.applyOperation({ type: 'test' } as any, { noApply: true });
  43. expect(handleApply).toBeCalledTimes(2);
  44. });
  45. it('apply with origin', () => {
  46. const handleApply = vi.fn();
  47. operationService.onApply(handleApply);
  48. const op = { type: 'test', origin: Symbol('origin'), value: {} };
  49. operationService.applyOperation(op);
  50. expect(handleApply).toBeCalledWith(op);
  51. });
  52. });