Skip to content

Commit

Permalink
test: repository stub
Browse files Browse the repository at this point in the history
  • Loading branch information
falkoschumann committed Jun 18, 2024
1 parent 3c6c374 commit ea933cb
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 15 deletions.
21 changes: 12 additions & 9 deletions packages/webserver/src/infrastructure/repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,7 @@ export class Repository extends EventTarget {
}

async record(/** @type {ActivityLogged} */ event) {
const dto = {
Timestamp: event.timestamp.toISOString(),
Duration: event.duration.toISOString(),
Client: event.client,
Project: event.project,
Task: event.task,
Notes: event.notes,
};
const dto = ActivityLoggedDto.fromDomain(event);
const csv = await this.#stringifyCsv(dto);
await this.#writeFile(csv);
this.dispatchEvent(
Expand Down Expand Up @@ -131,6 +124,17 @@ export class ActivityLoggedDto {
);
}

static fromDomain(/** @type {ActivityLogged} */ event) {
return ActivityLoggedDto.create({
Timestamp: event.timestamp.toISOString(),
Duration: event.duration.toISOString(),
Client: event.client,
Project: event.project,
Task: event.task,
Notes: event.notes,
});
}

constructor(
/** @type {string} */ Timestamp,
/** @type {string} */ Duration,
Expand All @@ -148,7 +152,6 @@ export class ActivityLoggedDto {
}

validate() {
// TODO throw ValidationError with DTO details
const timestamp = validateRequiredProperty(
this,
'activity logged',
Expand Down
43 changes: 37 additions & 6 deletions packages/webserver/test/integration/repository.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ describe('Repository', () => {
Notes: 'Show my recent activities',
});

expect(() => dto.validate()).toThrow(ValidationError);
expect(() => dto.validate()).toThrow(
'The property "Timestamp" of activity logged must be a valid Date, found string: "2024-13-02T11:35Z".',
);
});

test('Reports an error, if duration is invalid', async () => {
Expand All @@ -71,7 +73,9 @@ describe('Repository', () => {
Notes: 'Show my recent activities',
});

expect(() => dto.validate()).toThrow(ValidationError);
expect(() => dto.validate()).toThrow(
'The property "Duration" of activity logged must be a valid Duration, found string: "30m".',
);
});

test('Reports an error, if client is missing', async () => {
Expand All @@ -83,7 +87,9 @@ describe('Repository', () => {
Notes: 'Show my recent activities',
});

expect(() => dto.validate()).toThrow(ValidationError);
expect(() => dto.validate()).toThrow(
'The property "Client" is required for activity logged.',
);
});

test('Reports an error, if project is missing', async () => {
Expand All @@ -95,7 +101,9 @@ describe('Repository', () => {
Notes: 'Show my recent activities',
});

expect(() => dto.validate()).toThrow(ValidationError);
expect(() => dto.validate()).toThrow(
'The property "Project" is required for activity logged.',
);
});

test('Reports an error, if task is missing', async () => {
Expand All @@ -107,7 +115,9 @@ describe('Repository', () => {
Notes: 'Show my recent activities',
});

expect(() => dto.validate()).toThrow(ValidationError);
expect(() => dto.validate()).toThrow(
'The property "Task" is required for activity logged.',
);
});

test('Reports no error, if notes is missing', async () => {
Expand All @@ -124,7 +134,7 @@ describe('Repository', () => {
});
});

describe('Add', () => {
describe('Record', () => {
test('Creates file, if it does not exist', async () => {
const { repository } = await configure({
filename: '../../data/activity-log.test.csv',
Expand Down Expand Up @@ -153,6 +163,27 @@ describe('Repository', () => {
expect(events).toEqual([event1, event2]);
});
});

describe('Stub', () => {
test('Tracks events recorded', async () => {
const repository = Repository.createNull();
const eventsRecorded = repository.trackEventsRecorded();
const event = ActivityLogged.createNull();

await repository.record(event);

expect(eventsRecorded.data).toEqual([event]);
});

test('Replays events', async () => {
const event = ActivityLogged.createNull();
const repository = Repository.createNull({ events: [event] });

const events = await repository.replay();

expect(events).toEqual([event]);
});
});
});

async function configure({
Expand Down

0 comments on commit ea933cb

Please sign in to comment.