Skip to content

Commit

Permalink
adding more channel support
Browse files Browse the repository at this point in the history
  • Loading branch information
boyney123 committed Nov 12, 2024
1 parent 20895ad commit 81e9e41
Show file tree
Hide file tree
Showing 2 changed files with 248 additions and 0 deletions.
83 changes: 83 additions & 0 deletions src/utils/__tests__/channels/channels.spec.ts
Original file line number Diff line number Diff line change
@@ -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<typeof import('astro:content')>()),
// 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',
},
],
}),
}),
])
);
});
});
});
165 changes: 165 additions & 0 deletions src/utils/__tests__/channels/mocks.ts
Original file line number Diff line number Diff line change
@@ -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',
},
},
];

0 comments on commit 81e9e41

Please sign in to comment.