-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(domain): test the newsletter service
- Loading branch information
1 parent
a49bee6
commit af61462
Showing
3 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
packages/domain/__tests__/services/newsletters/newsletter.service.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import SharinganError from '@sharingan/utils'; | ||
import nock from 'nock'; | ||
|
||
import NewsletterService from '../../../src/newsletters/newsletter.service'; | ||
|
||
const newsletterService = new NewsletterService({ | ||
apiKey: 'apiKey', | ||
formId: 'formId', | ||
}); | ||
|
||
const baseURL = 'https://api.convertkit.com/v3'; | ||
|
||
describe('Test the newsletter service', () => { | ||
test('Add the email address to the newsletter subscribers', async () => { | ||
// GIVEN | ||
const emailToSubscribe = '[email protected]'; | ||
const tags = ['sharingan']; | ||
const formId = 'formId'; | ||
|
||
const scope = nock(baseURL) | ||
.post(`/forms/${formId}/subscribe`, { | ||
api_key: 'apiKey', | ||
email: emailToSubscribe, | ||
tags, | ||
}) | ||
.reply(200, { | ||
subscription: { | ||
id: '123ABC', | ||
}, | ||
}); | ||
|
||
// WHEN | ||
await newsletterService.subscribe(emailToSubscribe, tags); | ||
|
||
// THEN | ||
expect(scope.isDone()).toBe(true); | ||
|
||
nock.cleanAll(); | ||
}); | ||
|
||
test('Handle HTTP error when the request to add the email address to the newsletter subscribers fails', async () => { | ||
// GIVEN | ||
const emailToSubscribe = '[email protected]'; | ||
const tags = ['sharingan']; | ||
const formId = 'formId'; | ||
|
||
nock(baseURL) | ||
.post(`/forms/${formId}/subscribe`, { | ||
api_key: 'apiKey', | ||
email: emailToSubscribe, | ||
tags, | ||
}) | ||
.reply(400, { | ||
message: 'Wrong api key provided!', | ||
}); | ||
|
||
// WHEN | ||
// THEN | ||
const catchErrorsFormatted = { | ||
data: { | ||
message: 'Wrong api key provided!', | ||
}, | ||
message: 'Request failed with status code 400', | ||
status: 400, | ||
}; | ||
|
||
await expect(async () => { | ||
await newsletterService.subscribe(emailToSubscribe, tags); | ||
}).rejects.toThrow( | ||
new SharinganError(JSON.stringify(catchErrorsFormatted, null, 2), 'NEWSLETTER_SUBSCRIBE_FAILED'), | ||
); | ||
|
||
nock.cleanAll(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters