Skip to content

Commit

Permalink
refactor: replace should with node:assert in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jrassa committed Sep 18, 2024
1 parent 627f09e commit 47af7d0
Show file tree
Hide file tree
Showing 29 changed files with 1,460 additions and 1,509 deletions.
94 changes: 0 additions & 94 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@
"nodemon": "^3.1.4",
"nyc": "^17.0.0",
"prettier": "^3.0.0",
"should": "^13.2.3",
"sinon": "^18.0.0",
"swagger-parser": "^10.0.3"
},
Expand Down
46 changes: 27 additions & 19 deletions src/app/common/mongoose/contains-search.plugin.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import assert from 'node:assert/strict';

import { model, Model, Schema } from 'mongoose';
import should from 'should';

import {
ContainsSearchable,
Expand Down Expand Up @@ -40,18 +41,17 @@ describe('Contains Search Plugin:', () => {
describe('containsSearch:', () => {
it('should add containsSearch function to query', () => {
const query = ContainsExample.find();
should.exist(query.containsSearch);

query.containsSearch.should.be.Function();
assert(query.containsSearch);
assert.equal(typeof query.containsSearch, 'function');
});

it('should not add to filter if search term is null/undefined/empty string', () => {
[null, undefined, ''].forEach((search) => {
const query = ContainsExample.find().containsSearch(search);

const filter = query.getFilter();
should.exist(filter);
should.not.exist(filter.$or);

assert.deepStrictEqual(filter, {});
});
});

Expand All @@ -60,8 +60,8 @@ describe('Contains Search Plugin:', () => {
const query = ContainsExample.find().containsSearch('test', fields);

const filter = query.getFilter();
should.exist(filter);
should.not.exist(filter.$or);

assert.deepStrictEqual(filter, {});
});
});

Expand All @@ -73,29 +73,37 @@ describe('Contains Search Plugin:', () => {
]);

const filter = query.getFilter();
should.exist(filter);
should.exist(filter.$or);
filter.$or.should.be.an.Array();
filter.$or.length.should.equal(3);

assert.deepStrictEqual(filter, {
$or: [
{ field1: { $regex: /test/gim } },
{ field2: { $regex: /test/gim } },
{ field3: { $regex: /test/gim } }
]
});
});

it('should not create $or if only one field in list', () => {
const query = ContainsExample.find().containsSearch('test', ['field1']);

const filter = query.getFilter();
should.exist(filter);
should.not.exist(filter.$or);
should.exist(filter.field1);

assert.deepStrictEqual(filter, {
field1: { $regex: /test/gim }
});
});

it('should use default fields list in pluginOptions', () => {
const query = ContainsExample2.find().containsSearch('test');

const filter = query.getFilter();
should.exist(filter);
should.exist(filter.$or);
filter.$or.should.be.Array();
filter.$or.length.should.equal(2);

assert.deepStrictEqual(filter, {
$or: [
{ field1: { $regex: /test/gim } },
{ field2: { $regex: /test/gim } }
]
});
});
});
});
59 changes: 33 additions & 26 deletions src/app/common/mongoose/paginate.plugin.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import assert from 'node:assert/strict';

import { HydratedDocument, model, Model, Schema } from 'mongoose';
import should from 'should';

import { Paginateable, paginatePlugin } from './paginate.plugin';

Expand Down Expand Up @@ -40,59 +41,65 @@ describe('Paging Search Plugin:', () => {

it('should add paginate function to query', () => {
const query = PaginateExample.find();
should.exist(query.paginate);

query.paginate.should.be.Function();
assert(query.paginate);
assert.equal(typeof query.paginate, 'function');
});

it('should return promise', () => {
const promise = PaginateExample.find().paginate(10, 1);

promise.then.should.be.an.instanceof(Function);
assert(promise instanceof Promise);
});

it('should return specified page of data', async () => {
const result = await PaginateExample.find().paginate(10, 5);
should.exist(result);
result.should.be.containEql({
const { elements, ...result } = await PaginateExample.find().paginate(
10,
5
);
assert.deepStrictEqual(result, {
pageSize: 10,
pageNumber: 5,
totalSize: 100,
totalPages: 10
});
should.exist(result.elements);
result.elements.should.be.an.Array();
result.elements.length.should.equal(10);
assert(elements);
assert(Array.isArray(elements), 'elements should be an Array');
assert.equal(elements.length, 10);
});

it('should return empty/first page if no data found', async () => {
const result = await PaginateExample.find({ field: 'invalid' }).paginate(
10,
5
);
should.exist(result);
result.should.be.containEql({
const { elements, ...result } = await PaginateExample.find({
field: 'invalid'
}).paginate(10, 5);

assert.deepStrictEqual(result, {
pageSize: 10,
pageNumber: 0,
totalSize: 0,
totalPages: 0
});
should.exist(result.elements);
result.elements.should.be.an.Array();
result.elements.length.should.equal(0);

assert(elements);
assert(Array.isArray(elements), 'elements should be an Array');
assert.equal(elements.length, 0);
});

it('should return page with expected metadata when runCount = false', async () => {
const result = await PaginateExample.find().paginate(10, 5, false);
should.exist(result);
result.should.be.containEql({
const { elements, ...result } = await PaginateExample.find().paginate(
10,
5,
false
);

assert.deepStrictEqual(result, {
pageSize: 10,
pageNumber: 5,
totalSize: Number.MAX_SAFE_INTEGER,
totalPages: Math.ceil(Number.MAX_SAFE_INTEGER / 10)
});
should.exist(result.elements);
result.elements.should.be.an.Array();
result.elements.length.should.equal(10);

assert(elements);
assert(Array.isArray(elements), 'elements should be an Array');
assert.equal(elements.length, 10);
});
});
Loading

0 comments on commit 47af7d0

Please sign in to comment.