diff --git a/src/utils/__tests__/channels/channels.spec.ts b/src/utils/__tests__/channels/channels.spec.ts new file mode 100644 index 00000000..685cac1a --- /dev/null +++ b/src/utils/__tests__/channels/channels.spec.ts @@ -0,0 +1,83 @@ +import type { ContentCollectionKey } from 'astro:content'; +import { expect, describe, it, vi } from 'vitest'; +import { mockCommands, mockEvents, mockQueries, mockServices, mockChannels } from './mocks'; +import { getChannels } from '@utils/channels'; + +vi.mock('astro:content', async (importOriginal) => { + return { + ...(await importOriginal()), + // this will only affect "foo" outside of the original module + getCollection: (key: ContentCollectionKey) => { + switch (key) { + case 'services': + return Promise.resolve(mockServices); + case 'events': + return Promise.resolve(mockEvents); + case 'commands': + return Promise.resolve(mockCommands); + case 'channels': + return Promise.resolve(mockChannels); + case 'queries': + return Promise.resolve(mockQueries); + } + }, + }; +}); + +describe('channels', () => { + describe('getChannels', () => { + it('returns an array of channels', async () => { + const channels = await getChannels(); + + expect(channels).toEqual( + expect.arrayContaining([ + expect.objectContaining({ + collection: 'channels', + data: expect.objectContaining({ + id: 'orders.{env}.events', + version: '1.0.0', + }), + }), + expect.objectContaining({ + collection: 'channels', + data: expect.objectContaining({ + id: 'inventory.{env}.events', + version: '1.0.0', + }), + }), + ]) + ); + }); + + it('should returns an array of channels with messages', async () => { + const channels = await getChannels(); + + expect(channels).toEqual( + expect.arrayContaining([ + /** OrderService */ + expect.objectContaining({ + id: 'orders.{env}.events', + slug: 'orders.{env}.events', + collection: 'channels', + data: expect.objectContaining({ + messages: [ + { + id: 'OrderCreatedEvent', + name: 'Order Created', + version: '0.0.1', + collection: 'events', + }, + { + id: 'GetOrder', + name: 'Get Order', + version: '0.0.1', + collection: 'queries', + }, + ], + }), + }), + ]) + ); + }); + }); +}); diff --git a/src/utils/__tests__/channels/mocks.ts b/src/utils/__tests__/channels/mocks.ts new file mode 100644 index 00000000..fe06b68c --- /dev/null +++ b/src/utils/__tests__/channels/mocks.ts @@ -0,0 +1,165 @@ +export const mockServices = [ + { + id: 'services/Order/OrderService/index.mdx', + slug: 'services/Order/OrderService', + collection: 'services', + data: { + id: 'OrderService', + version: '1.0.0', + sends: [ + { + id: 'OrderCreatedEvent', + name: 'Order Created', + version: '0.0.1', + }, + ], + receives: [ + { + id: 'PaymentProcessed', + name: 'Payment Processed', + version: '0.0.1', + }, + ], + }, + }, + { + id: 'services/Inventory/InventoryService/index.mdx', + slug: 'services/Inventory/InventoryService', + collection: 'services', + data: { + id: 'InventoryService', + version: '1.0.0', + receives: [ + { + id: 'OrderCreatedEvent', + name: 'Order Created', + version: '^1.3.0', + }, + ], + sends: [ + { + id: 'InventoryAdjusted', + version: '~2', + }, + ], + }, + }, + { + id: 'services/Payment/PaymentService/index.mdx', + slug: 'services/Payment/PaymentService', + collection: 'services', + data: { + id: 'PaymentService', + version: '1.0.0', + receives: [ + { + id: 'OrderCreatedEvent', + }, + { + id: 'OrderDeletedEvent', + }, + ], + sends: [ + { + id: 'PaymentPaid', + }, + { + id: 'PaymentFailed', + version: 'latest', + }, + { + id: 'EmailVerified', + }, + ], + }, + }, + { + id: 'services/Notifications/NotificationsService/index.mdx', + slug: 'services/Notifications/NotificationsService', + collection: 'services', + data: { + id: 'NotificationsService', + version: '1.0.0', + receives: [ + { + id: 'OrderCreatedEvent', + }, + ], + sends: [ + { + id: 'OrderCreatedEvent', + }, + ], + }, + }, +]; + +export const mockEvents = [ + { + id: 'OrderCreatedEvent', + slug: 'OrderCreatedEvent', + collection: 'events', + data: { + id: 'OrderCreatedEvent', + version: '0.0.1', + name: 'Order Created', + channels: [ + { + id: 'orders.{env}.events', + version: '1.0.0', + }, + ], + }, + }, +]; + +export const mockCommands = [ + { + id: 'PaymentProcessed', + slug: 'PaymentProcessed', + collection: 'commands', + data: { + id: 'PaymentProcessed', + version: '0.0.1', + }, + }, +]; +export const mockQueries = [ + { + id: 'GetOrder', + slug: 'GetOrder', + collection: 'queries', + data: { + id: 'GetOrder', + version: '0.0.1', + name: 'Get Order', + channels: [ + { + id: 'orders.{env}.events', + version: '1.0.0', + }, + ], + }, + }, +]; + +export const mockChannels = [ + { + id: 'orders.{env}.events', + slug: 'orders.{env}.events', + collection: 'channels', + data: { + id: 'orders.{env}.events', + version: '1.0.0', + }, + }, + { + id: 'inventory.{env}.events', + slug: 'inventory.{env}.events', + collection: 'channels', + data: { + id: 'inventory.{env}.events', + version: '1.0.0', + }, + }, +];