-
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.
* Pagination of all routes * Finally separating querification from validation - Solves #33 & #50 - More simple tests for helpers.ts
- Loading branch information
Showing
29 changed files
with
361 additions
and
115 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ test.drawio | |
A&D - MVP.md | ||
out/ | ||
test.ts | ||
snippet.ts | ||
package-lock.json | ||
coverage/ | ||
uploads/ | ||
|
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
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
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,59 @@ | ||
// Unit tests for the utils/helpers.ts file | ||
|
||
import { Op } from 'sequelize'; | ||
import { getPagination, querifyStringFields } from '../utils/helpers'; | ||
import { ZSubjectFind } from '../validator/subject.validator'; | ||
import { ZStudyClassFind } from '../validator/studyClass.validator'; | ||
|
||
// This was made with the help of Github Copilot (first time using it) | ||
describe('getPagination() function unit tests', () => { | ||
test('should return an object with offset and limit', () => { | ||
const obj = getPagination({ page: 1, size: 10 }); | ||
|
||
expect(obj).toHaveProperty('offset'); | ||
expect(obj).toHaveProperty('limit'); | ||
|
||
expect(obj.offset).toBe(0); | ||
expect(obj.limit).toBe(10); | ||
}); | ||
|
||
test('getPagination() returns rest of the object', () => { | ||
const obj = getPagination({ page: 1, size: 10, name: 'test', age: 2 }); | ||
|
||
expect(obj.rest).toEqual({ name: 'test', age: 2 }); | ||
}); | ||
}); | ||
|
||
describe('querifyStringFields() function unit tests', () => { | ||
test('Try on Subject model', () => { | ||
// You can do it like this: | ||
const { query } = ZSubjectFind.parse({ | ||
query: { name: 'test', subjectId: '123' }, | ||
}); | ||
|
||
const querified = querifyStringFields(query, ZSubjectFind.shape.query); | ||
|
||
expect(querified.name).toStrictEqual({ [Op.like]: '%test%' }); | ||
|
||
// subjectId is ZodString, that's why it is "querified". | ||
expect(querified.subjectId).toStrictEqual({ [Op.like]: '%123%' }); | ||
expect(querified.page).toBe(1); | ||
expect(querified.size).toBe(10); | ||
}); | ||
|
||
test('Try on StudyClass model', () => { | ||
// or like this: | ||
const query = ZStudyClassFind.shape.query.parse({ | ||
educationType: 'Literature', | ||
studyYear: '1', | ||
classId: '123', | ||
}); | ||
|
||
const querified = querifyStringFields(query, ZStudyClassFind.shape.query); | ||
|
||
// Enums stay the same. | ||
expect(querified.educationType).toStrictEqual('Literature'); | ||
expect(querified.studyYear).toStrictEqual('1'); | ||
expect(querified.classId).toStrictEqual({ [Op.like]: '%123%' }); | ||
}); | ||
}); |
Oops, something went wrong.