123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- import 'reflect-metadata'
- import {
- DomainEventPublisherInterface,
- DomainEventService,
- ItemRevisionCreationRequestedEvent,
- } from '@standardnotes/domain-events'
- import { Item } from '../Item/Item'
- import { ItemRepositoryInterface } from '../Item/ItemRepositoryInterface'
- import { ItemRevisionCreationRequestedEventHandler } from './ItemRevisionCreationRequestedEventHandler'
- import { ItemBackupServiceInterface } from '../Item/ItemBackupServiceInterface'
- import { DomainEventFactoryInterface } from '../Event/DomainEventFactoryInterface'
- import { Uuid, ContentType, Dates, Timestamps, UniqueEntityId } from '@standardnotes/domain-core'
- import { ItemRepositoryResolverInterface } from '../Item/ItemRepositoryResolverInterface'
- describe('ItemRevisionCreationRequestedEventHandler', () => {
- let itemRepositoryResolver: ItemRepositoryResolverInterface
- let itemRepository: ItemRepositoryInterface
- let event: ItemRevisionCreationRequestedEvent
- let item: Item
- let itemBackupService: ItemBackupServiceInterface
- let domainEventFactory: DomainEventFactoryInterface
- let domainEventPublisher: DomainEventPublisherInterface
- const createHandler = () =>
- new ItemRevisionCreationRequestedEventHandler(
- itemRepositoryResolver,
- itemBackupService,
- domainEventFactory,
- domainEventPublisher,
- )
- beforeEach(() => {
- item = Item.create(
- {
- userUuid: Uuid.create('00000000-0000-0000-0000-000000000000').getValue(),
- updatedWithSession: null,
- content: 'foobar1',
- contentType: ContentType.create(ContentType.TYPES.Note).getValue(),
- encItemKey: null,
- authHash: null,
- itemsKeyId: null,
- duplicateOf: null,
- deleted: false,
- dates: Dates.create(new Date(1616164633241311), new Date(1616164633241311)).getValue(),
- timestamps: Timestamps.create(1616164633241311, 1616164633241311).getValue(),
- },
- new UniqueEntityId('00000000-0000-0000-0000-000000000000'),
- ).getValue()
- itemRepository = {} as jest.Mocked<ItemRepositoryInterface>
- itemRepository.findByUuid = jest.fn().mockReturnValue(item)
- itemRepositoryResolver = {} as jest.Mocked<ItemRepositoryResolverInterface>
- itemRepositoryResolver.resolve = jest.fn().mockReturnValue(itemRepository)
- event = {} as jest.Mocked<ItemRevisionCreationRequestedEvent>
- event.createdAt = new Date(1)
- event.payload = {
- itemUuid: '00000000-0000-0000-0000-000000000000',
- roleNames: ['CORE_USER'],
- }
- event.meta = {
- correlation: {
- userIdentifier: '1-2-3',
- userIdentifierType: 'uuid',
- },
- origin: DomainEventService.SyncingServer,
- }
- itemBackupService = {} as jest.Mocked<ItemBackupServiceInterface>
- itemBackupService.dump = jest.fn().mockReturnValue('foo://bar')
- domainEventFactory = {} as jest.Mocked<DomainEventFactoryInterface>
- domainEventFactory.createItemDumpedEvent = jest.fn()
- domainEventPublisher = {} as jest.Mocked<DomainEventPublisherInterface>
- domainEventPublisher.publish = jest.fn()
- })
- it('should create a revision for an item', async () => {
- await createHandler().handle(event)
- expect(domainEventPublisher.publish).toHaveBeenCalled()
- expect(domainEventFactory.createItemDumpedEvent).toHaveBeenCalled()
- })
- it('should do nothing if roles names are not valid', async () => {
- event.payload.roleNames = ['INVALID_ROLE_NAME']
- await createHandler().handle(event)
- expect(domainEventPublisher.publish).not.toHaveBeenCalled()
- expect(domainEventFactory.createItemDumpedEvent).not.toHaveBeenCalled()
- })
- it('should not create a revision for an item that does not exist', async () => {
- itemRepository.findByUuid = jest.fn().mockReturnValue(null)
- itemRepositoryResolver.resolve = jest.fn().mockReturnValue(itemRepository)
- await createHandler().handle(event)
- expect(domainEventPublisher.publish).not.toHaveBeenCalled()
- })
- it('should not create a revision for an item if the dump was not created', async () => {
- itemBackupService.dump = jest.fn().mockReturnValue('')
- await createHandler().handle(event)
- expect(domainEventPublisher.publish).not.toHaveBeenCalled()
- expect(domainEventFactory.createItemDumpedEvent).not.toHaveBeenCalled()
- })
- it('should not create a revision if the item uuid is invalid', async () => {
- event.payload.itemUuid = 'invalid-uuid'
- await createHandler().handle(event)
- expect(domainEventPublisher.publish).not.toHaveBeenCalled()
- expect(domainEventFactory.createItemDumpedEvent).not.toHaveBeenCalled()
- })
- })
|