Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/eventsourcing-demo/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
]
},
"imports": {
"@std/assert": "jsr:@std/assert@^1.0.10",
"@std/dotenv": "jsr:@std/dotenv@^0.225.6",
"@std/ulid": "jsr:@std/ulid@^1.0.0",
"eventsourcingdb": "npm:eventsourcingdb@^1.8.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { createCommand, createEvent } from '@nimbus/core';
import { createScenario } from '@nimbus/eventsourcingdb';
import { applyEventToUserState, type UserState } from './user.state.ts';
import {
INVITE_USER_COMMAND_TYPE,
type InviteUserCommand,
} from '../commands/inviteUser.command.ts';
import {
ACCEPT_USER_INVITATION_COMMAND_TYPE,
type AcceptUserInvitationCommand,
} from '../commands/acceptUserInvitation.command.ts';
import {
USER_INVITED_EVENT_TYPE,
type UserInvitedEvent,
} from '../events/userInvited.event.ts';

const TEST_SOURCE = 'https://test.overlap.at';

export const userScenario = () =>
createScenario<UserState>({ id: 'test-user-id' }, applyEventToUserState);

export const anInviteUserCommand = (
data: { email: string; firstName: string; lastName: string },
): InviteUserCommand =>
createCommand<InviteUserCommand>({
type: INVITE_USER_COMMAND_TYPE,
source: TEST_SOURCE,
data,
});

export const anAcceptUserInvitationCommand = (
data: { id: string; expectedRevision: string },
): AcceptUserInvitationCommand =>
createCommand<AcceptUserInvitationCommand>({
type: ACCEPT_USER_INVITATION_COMMAND_TYPE,
source: TEST_SOURCE,
data,
});

export const aUserInvitedEvent = (
data: {
id: string;
email: string;
firstName: string;
lastName: string;
invitedAt: string;
},
): UserInvitedEvent =>
createEvent<UserInvitedEvent>({
type: USER_INVITED_EVENT_TYPE,
source: TEST_SOURCE,
subject: `/users/${data.id}`,
data,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import { inviteUser } from '../commands/inviteUser.command.ts';
import { acceptUserInvitation } from '../commands/acceptUserInvitation.command.ts';
import { USER_INVITED_EVENT_TYPE } from '../events/userInvited.event.ts';
import { USER_INVITATION_ACCEPTED_EVENT_TYPE } from '../events/userInvitationAccepted.event.ts';
import {
anAcceptUserInvitationCommand,
anInviteUserCommand,
aUserInvitedEvent,
userScenario,
} from './user.test.helper.ts';

Deno.test('inviteUser emits UserInvited event', () => {
userScenario()
.given([])
.when((state) =>
inviteUser(
state,
anInviteUserCommand({
email: 'jane@example.com',
firstName: 'Jane',
lastName: 'Doe',
}),
)
)
.then([{
type: USER_INVITED_EVENT_TYPE,
data: {
email: 'jane@example.com',
firstName: 'Jane',
lastName: 'Doe',
},
}]);
});

Deno.test('inviteUser lowercases email', () => {
userScenario()
.given([])
.when((state) =>
inviteUser(
state,
anInviteUserCommand({
email: 'Jane@Example.COM',
firstName: 'Jane',
lastName: 'Doe',
}),
)
)
.then([{
type: USER_INVITED_EVENT_TYPE,
data: { email: 'jane@example.com' },
}]);
});

Deno.test('acceptUserInvitation emits UserInvitationAccepted event', () => {
userScenario()
.given([
aUserInvitedEvent({
id: 'test-user-id',
email: 'jane@example.com',
firstName: 'Jane',
lastName: 'Doe',
invitedAt: new Date().toISOString(),
}),
])
.when((state) =>
acceptUserInvitation(
state,
anAcceptUserInvitationCommand({
id: 'test-user-id',
expectedRevision: 'any',
}),
)
)
.then([{
type: USER_INVITATION_ACCEPTED_EVENT_TYPE,
}]);
});

Deno.test('acceptUserInvitation throws if no pending invitation', () => {
userScenario()
.given([])
.when((state) =>
acceptUserInvitation(
state,
anAcceptUserInvitationCommand({
id: 'test-user-id',
expectedRevision: 'any',
}),
)
)
.thenThrows('USER_HAS_NO_PENDING_INVITATION');
});

Deno.test('acceptUserInvitation throws if invitation expired', () => {
const expiredDate = new Date(
Date.now() - 25 * 60 * 60 * 1000,
).toISOString();

userScenario()
.given([
aUserInvitedEvent({
id: 'test-user-id',
email: 'jane@example.com',
firstName: 'Jane',
lastName: 'Doe',
invitedAt: expiredDate,
}),
])
.when((state) =>
acceptUserInvitation(
state,
anAcceptUserInvitationCommand({
id: 'test-user-id',
expectedRevision: 'any',
}),
)
)
.thenThrows('INVITATION_EXPIRED');
});
1 change: 1 addition & 0 deletions packages/eventsourcingdb/deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
]
},
"imports": {
"@std/assert": "jsr:@std/assert@^1.0.10",
"eventsourcingdb": "npm:eventsourcingdb@^1.8.1"
}
}
1 change: 1 addition & 0 deletions packages/eventsourcingdb/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './lib/client.ts';
export * from './lib/scenario.ts';
Loading
Loading