Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
annarhughes committed Dec 3, 2024
1 parent 4c66f43 commit a23f372
Showing 1 changed file with 39 additions and 21 deletions.
60 changes: 39 additions & 21 deletions src/webhooks/webhooks.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,11 @@ describe('WebhooksService', () => {
});

const sessionSaveRepoSpy = jest.spyOn(mockedSessionRepository, 'save');
const sessionFindOneRepoSpy = jest.spyOn(mockedSessionRepository, 'findOneBy');
const sessionFindOneRepoSpy = jest
.spyOn(mockedSessionRepository, 'findOneBy')
.mockImplementationOnce(async () => {
return { ...mockSession, course: course2 };
});

const courseFindOneSpy = jest
.spyOn(mockedCourseRepository, 'findOneByOrFail')
Expand Down Expand Up @@ -304,21 +308,25 @@ describe('WebhooksService', () => {
const body = {
action: STORYBLOK_STORY_STATUS_ENUM.PUBLISHED,
story_id: mockSession.storyblokId,
full_slug: mockSession.slug,
text: '',
};

const expectedResponse = {
storyblokId: mockSession.storyblokId,
storyblokUuid: mockSession.storyblokUuid,
status: STORYBLOK_STORY_STATUS_ENUM.PUBLISHED,
slug: mockSession.slug,
name: mockSession.name,
};

const session = (await service.handleStoryUpdated(body)) as SessionEntity;

expect(session).toEqual(mockSession);
expect(session).toEqual(expectedResponse);
expect(courseFindOneSpy).toHaveBeenCalledWith({
storyblokUuid: 'courseUuid1',
});
expect(sessionSaveRepoSpy).toHaveBeenCalledWith({
...mockSession,
});
expect(sessionSaveRepoSpy).toHaveBeenCalledWith({
...mockSession,
});
expect(sessionSaveRepoSpy).toHaveBeenCalledWith(expectedResponse);
expect(sessionFindOneRepoSpy).toHaveBeenCalledWith({
storyblokId: mockSession.storyblokId,
});
Expand Down Expand Up @@ -361,15 +369,22 @@ describe('WebhooksService', () => {
const body = {
action: STORYBLOK_STORY_STATUS_ENUM.PUBLISHED,
story_id: mockSession.storyblokId,
full_slug: mockSession.slug,
text: '',
};

const expectedResponse = {
storyblokId: mockSession.storyblokId,
storyblokUuid: mockSession.storyblokUuid,
status: STORYBLOK_STORY_STATUS_ENUM.PUBLISHED,
slug: mockSession.slug,
name: mockSession.name,
};

const session = (await service.handleStoryUpdated(body)) as SessionEntity;

expect(session).toEqual(mockSession);
expect(sessionSaveRepoSpy).toHaveBeenCalledWith({
...mockSession,
});
expect(session).toEqual(expectedResponse);
expect(sessionSaveRepoSpy).toHaveBeenCalledWith(expectedResponse);

courseFindOneSpy.mockClear();
sessionSaveRepoSpy.mockClear();
Expand All @@ -392,24 +407,27 @@ describe('WebhooksService', () => {

const body = {
action: STORYBLOK_STORY_STATUS_ENUM.PUBLISHED,
story_id: 5678,
story_id: mockCourseStoryblokResult.data.story.id,
full_slug: mockCourseStoryblokResult.data.story.full_slug,
text: '',
};

const expectedResponse = {
storyblokId: mockCourseStoryblokResult.data.story.id,
storyblokUuid: mockCourseStoryblokResult.data.story.uuid,
status: STORYBLOK_STORY_STATUS_ENUM.PUBLISHED,
slug: mockCourseStoryblokResult.data.story.full_slug,
name: mockCourseStoryblokResult.data.story.name,
};

const course = (await service.handleStoryUpdated(body)) as CourseEntity;

expect(course).toEqual(mockCourse);
expect(course).toEqual(expectedResponse);
expect(courseFindOneRepoSpy).toHaveBeenCalledWith({
storyblokId: mockCourseStoryblokResult.data.story.id,
});

expect(courseSaveRepoSpy).toHaveBeenCalledWith({
storyblokId: mockCourseStoryblokResult.data.story.id,
name: mockCourseStoryblokResult.data.story.name,
status: STORYBLOK_STORY_STATUS_ENUM.PUBLISHED,
slug: mockCourseStoryblokResult.data.story.full_slug,
storyblokUuid: mockCourseStoryblokResult.data.story.uuid,
});
expect(courseSaveRepoSpy).toHaveBeenCalledWith(expectedResponse);

expect(mockedServiceUserProfilesService.createMailchimpCourseMergeField).toHaveBeenCalledWith(
mockCourse.name,
Expand Down

0 comments on commit a23f372

Please sign in to comment.