-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(general): test everything possible, 100% coverage
- Loading branch information
1 parent
0410abe
commit 40dd4b5
Showing
5 changed files
with
108 additions
and
3 deletions.
There are no files selected for viewing
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
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
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 @@ | ||
const moment = require('moment'); | ||
|
||
const { startServer, stopServer } = require('../../lib/server.js'); | ||
const generator = require('../scripts/generator'); | ||
const cron = require('../../lib/cron'); | ||
const { User } = require('../../models'); | ||
|
||
describe('Cron testing', () => { | ||
beforeAll(async () => { | ||
await startServer(); | ||
}); | ||
|
||
afterAll(async () => { | ||
await stopServer(); | ||
}); | ||
|
||
afterEach(async () => { | ||
await generator.clearAll(); | ||
await cron.clearAll(); | ||
}); | ||
|
||
test('should add job', async () => { | ||
expect(Object.keys(cron.jobs).length).toEqual(0);; | ||
|
||
cron.addJob(cron.JOB_TYPES.DELETE_NOT_CONFIRMED_USERS); | ||
expect(Object.keys(cron.jobs).length).toEqual(1); | ||
}); | ||
|
||
test('should execute job', async () => { | ||
expect(Object.keys(cron.jobs).length).toEqual(0);; | ||
|
||
cron.addJob(cron.JOB_TYPES.DELETE_NOT_CONFIRMED_USERS); | ||
expect(Object.keys(cron.jobs).length).toEqual(1); | ||
|
||
const jobKey = Object.keys(cron.jobs)[0]; | ||
await cron.executeJob(jobKey); | ||
}); | ||
|
||
test('should not execute a job if it\'s not found', async () => { | ||
expect(Object.keys(cron.jobs).length).toEqual(0);; | ||
await cron.executeJob(1337); | ||
}); | ||
|
||
test('should register all tasks', async () => { | ||
expect(Object.keys(cron.jobs).length).toEqual(0);; | ||
|
||
cron.registerAllTasks(); | ||
expect(Object.keys(cron.jobs).length).toEqual(1); | ||
}); | ||
|
||
describe('DELETE_NOT_CONFIRMED_USERS', () => { | ||
test('should work properly', async () => { | ||
const userActivated = await generator.createUser(); | ||
const userWithValidConfirmation = await generator.createUser(); | ||
const userWithoutValidConfirmation = await generator.createUser(); | ||
|
||
await generator.createMailConfirmation({ expires_at: moment().add(1, 'day' ) }, userWithValidConfirmation); | ||
await generator.createMailConfirmation({ expires_at: moment().subtract(1, 'day' ) }, userWithoutValidConfirmation); | ||
|
||
cron.addJob(cron.JOB_TYPES.DELETE_NOT_CONFIRMED_USERS); | ||
expect(Object.keys(cron.jobs).length).toEqual(1); | ||
|
||
const jobKey = Object.keys(cron.jobs)[0]; | ||
await cron.executeJob(jobKey); | ||
|
||
// first and second should be left as they are, third should be removed. | ||
const usersFromDb = await User.findAll(); | ||
expect(usersFromDb.length).toEqual(2); | ||
|
||
const ids = usersFromDb.map(u => u.id); | ||
expect(ids).toContain(userActivated.id); | ||
expect(ids).toContain(userWithValidConfirmation.id); | ||
}); | ||
}); | ||
}); |