Skip to content
Open
243 changes: 103 additions & 140 deletions apis/api-journeys-modern/src/workers/email/service/service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ApolloClient, ApolloQueryResult } from '@apollo/client'
import { Job } from 'bullmq'

import {
Expand All @@ -21,7 +20,18 @@ import {
} from './prisma.types'
import { service } from './service'

jest.mock('@apollo/client')
jest.mock('@core/prisma/users/client', () => ({
prisma: {
user: {
findUnique: jest.fn(),
findMany: jest.fn()
}
}
}))

const { prisma: mockPrismaUsers } = jest.requireMock(
'@core/prisma/users/client'
)

let args = {}
jest.mock('@core/yoga/email', () => ({
Expand Down Expand Up @@ -182,17 +192,12 @@ describe('EmailConsumer', () => {

describe('teamRemovedEmail', () => {
it('should send an email', async () => {
jest.spyOn(ApolloClient.prototype, 'query').mockImplementationOnce(
async () =>
await Promise.resolve({
data: {
user: {
id: 'userid',
email: '[email protected]'
}
}
} as unknown as ApolloQueryResult<unknown>)
)
mockPrismaUsers.user.findUnique.mockResolvedValueOnce({
id: 'userid',
email: '[email protected]',
firstName: 'John',
imageUrl: null
})
await service(teamRemoved)
expect(sendEmail).toHaveBeenCalled()
expect(args).toEqual({
Expand All @@ -210,35 +215,25 @@ describe('EmailConsumer', () => {
accountNotifications: false
})

jest.spyOn(ApolloClient.prototype, 'query').mockImplementationOnce(
async () =>
await Promise.resolve({
data: {
user: {
id: 'userid',
email: '[email protected]'
}
}
} as unknown as ApolloQueryResult<unknown>)
)
mockPrismaUsers.user.findUnique.mockResolvedValueOnce({
id: 'userid',
email: '[email protected]',
firstName: 'John',
imageUrl: null
})
await service(teamRemoved)
expect(sendEmail).not.toHaveBeenCalled()
})
})

describe('teamInviteEmail', () => {
it('should send an email if user exists', async () => {
jest.spyOn(ApolloClient.prototype, 'query').mockImplementationOnce(
async () =>
await Promise.resolve({
data: {
user: {
id: 'userid',
email: '[email protected]'
}
}
} as unknown as ApolloQueryResult<unknown>)
)
mockPrismaUsers.user.findUnique.mockResolvedValueOnce({
id: 'userid',
email: '[email protected]',
firstName: 'John',
imageUrl: null
})
await service(teamInviteJob)
expect(sendEmail).toHaveBeenCalled()
expect(args).toEqual({
Expand All @@ -250,14 +245,7 @@ describe('EmailConsumer', () => {
})

it('should send an email if user does not exist', async () => {
jest.spyOn(ApolloClient.prototype, 'query').mockImplementationOnce(
async () =>
await Promise.resolve({
data: {
user: undefined
}
} as unknown as ApolloQueryResult<unknown>)
)
mockPrismaUsers.user.findUnique.mockResolvedValueOnce(null)
await service(teamInviteJob)
expect(sendEmail).toHaveBeenCalled()
expect(args).toEqual({
Expand All @@ -282,18 +270,24 @@ describe('EmailConsumer', () => {

describe('teamInviteAcceptedEmail', () => {
it('should send an email', async () => {
jest.spyOn(ApolloClient.prototype, 'query').mockImplementation(
async () =>
await Promise.resolve({
data: {
user: {
id: 'userid',
email: '[email protected]'
}
}
} as unknown as ApolloQueryResult<unknown>)
)
mockPrismaUsers.user.findMany.mockResolvedValue([
{
id: 'userid',
userId: 'userId',
email: '[email protected]',
firstName: 'John',
imageUrl: null
},
{
id: 'userid2',
userId: 'userId2',
email: '[email protected]',
firstName: 'Jane',
imageUrl: null
}
])
await service(teamInviteAccepted)
expect(mockPrismaUsers.user.findMany).toHaveBeenCalledTimes(1)
expect(sendEmail).toHaveBeenCalledTimes(2)
expect(args).toEqual({
to: '[email protected]',
Expand All @@ -310,35 +304,36 @@ describe('EmailConsumer', () => {
accountNotifications: false
})

jest.spyOn(ApolloClient.prototype, 'query').mockImplementationOnce(
async () =>
await Promise.resolve({
data: {
user: {
id: 'userid',
email: '[email protected]'
}
}
} as unknown as ApolloQueryResult<unknown>)
)
mockPrismaUsers.user.findMany.mockResolvedValueOnce([
{
id: 'userid',
userId: 'userId',
email: '[email protected]',
firstName: 'John',
imageUrl: null
},
{
id: 'userid2',
userId: 'userId2',
email: '[email protected]',
firstName: 'Jane',
imageUrl: null
}
])
await service(teamInviteAccepted)
expect(mockPrismaUsers.user.findMany).toHaveBeenCalledTimes(1)
expect(sendEmail).not.toHaveBeenCalled()
})
})

describe('journeyAccessRequest', () => {
it('should send an email', async () => {
jest.spyOn(ApolloClient.prototype, 'query').mockImplementationOnce(
async () =>
await Promise.resolve({
data: {
user: {
id: 'userid',
email: '[email protected]'
}
}
} as unknown as ApolloQueryResult<unknown>)
)
mockPrismaUsers.user.findUnique.mockResolvedValueOnce({
id: 'userid',
email: '[email protected]',
firstName: 'John',
imageUrl: null
})
await service(journeyAccessRequest)
expect(sendEmail).toHaveBeenCalled()
expect(args).toEqual({
Expand All @@ -356,35 +351,25 @@ describe('EmailConsumer', () => {
accountNotifications: false
})

jest.spyOn(ApolloClient.prototype, 'query').mockImplementationOnce(
async () =>
await Promise.resolve({
data: {
user: {
id: 'userid',
email: '[email protected]'
}
}
} as unknown as ApolloQueryResult<unknown>)
)
mockPrismaUsers.user.findUnique.mockResolvedValueOnce({
id: 'userid',
email: '[email protected]',
firstName: 'John',
imageUrl: null
})
await service(journeyAccessRequest)
expect(sendEmail).not.toHaveBeenCalled()
})
})

describe('journeyRequestApproved', () => {
it('should send an email', async () => {
jest.spyOn(ApolloClient.prototype, 'query').mockImplementationOnce(
async () =>
await Promise.resolve({
data: {
user: {
id: 'userid',
email: '[email protected]'
}
}
} as unknown as ApolloQueryResult<unknown>)
)
mockPrismaUsers.user.findUnique.mockResolvedValueOnce({
id: 'userid',
email: '[email protected]',
firstName: 'John',
imageUrl: null
})
await service(journeyRequestApproved)
expect(sendEmail).toHaveBeenCalled()
expect(args).toEqual({
Expand All @@ -402,54 +387,37 @@ describe('EmailConsumer', () => {
accountNotifications: false
})

jest.spyOn(ApolloClient.prototype, 'query').mockImplementationOnce(
async () =>
await Promise.resolve({
data: {
user: {
id: 'userid',
email: '[email protected]'
}
}
} as unknown as ApolloQueryResult<unknown>)
)
mockPrismaUsers.user.findUnique.mockResolvedValueOnce({
id: 'userid',
email: '[email protected]',
firstName: 'John',
imageUrl: null
})
await service(journeyRequestApproved)
expect(sendEmail).not.toHaveBeenCalled()
})
})

describe('journeyEditInvite', () => {
it('should send an email if user exists', async () => {
jest.spyOn(ApolloClient.prototype, 'query').mockImplementationOnce(
async () =>
await Promise.resolve({
data: {
user: {
id: 'userid',
email: '[email protected]'
}
}
} as unknown as ApolloQueryResult<unknown>)
)
mockPrismaUsers.user.findUnique.mockResolvedValueOnce({
id: 'userid',
email: '[email protected]',
firstName: 'John',
imageUrl: null
})
await service(journeyEditJob)
expect(sendEmail).toHaveBeenCalled()
expect(args).toEqual({
to: journeyEditJob.data.email,
subject: 'Journey Title has been shared with you',
subject: 'Journey Title has been shared with you on NextSteps',
html: expect.any(String),
text: expect.any(String)
})
})

it('should send an email if user does not exist', async () => {
jest.spyOn(ApolloClient.prototype, 'query').mockImplementationOnce(
async () =>
await Promise.resolve({
data: {
user: undefined
}
} as unknown as ApolloQueryResult<unknown>)
)
mockPrismaUsers.user.findUnique.mockResolvedValueOnce(null)
await service(journeyEditJob)
expect(sendEmail).toHaveBeenCalled()
expect(args).toEqual({
Expand All @@ -467,17 +435,12 @@ describe('EmailConsumer', () => {
accountNotifications: false
})

jest.spyOn(ApolloClient.prototype, 'query').mockImplementationOnce(
async () =>
await Promise.resolve({
data: {
user: {
id: 'userid',
email: '[email protected]'
}
}
} as unknown as ApolloQueryResult<unknown>)
)
mockPrismaUsers.user.findUnique.mockResolvedValueOnce({
id: 'userid',
email: '[email protected]',
firstName: 'John',
imageUrl: null
})
await service(journeyEditJob)
expect(sendEmail).not.toHaveBeenCalled()
})
Expand Down
Loading
Loading