Skip to content

Commit

Permalink
fix unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
touv committed Aug 30, 2024
1 parent d2983ff commit 3bbd406
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/api/models/field.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import fieldFactory, {
import { URI_FIELD_NAME } from '../../common/uris';
import { SCOPE_DOCUMENT, SCOPE_COLLECTION } from '../../common/scope';

const listCollections = {
toArray: () => [true],
};

describe('field', () => {
describe('fieldFactory', () => {
let fieldCollection;
Expand Down Expand Up @@ -39,9 +43,9 @@ describe('field', () => {
Promise.resolve({ value: 'result' }),
),
};

db = {
collection: jest.fn().mockImplementation(() => fieldCollection),
listCollections: () => listCollections,
};

field = await fieldFactory(db);
Expand Down Expand Up @@ -285,10 +289,14 @@ describe('field', () => {
.mockImplementation(() => Promise.resolve({})),
insertOne: jest.fn(),
};
const listCollections = {
toArray: () => [true],
};
const dbNoUri = {
collection: jest
.fn()
.mockImplementation(() => fieldCollectionNoUri),
listCollections: () => listCollections,
};

const fieldNoUri = await fieldFactory(dbNoUri);
Expand Down Expand Up @@ -366,6 +374,7 @@ describe('field', () => {
collection: jest
.fn()
.mockImplementation(() => fieldCollection),
listCollections: () => listCollections,
};

field = await fieldFactory(db);
Expand Down Expand Up @@ -399,6 +408,7 @@ describe('field', () => {
collection: jest
.fn()
.mockImplementation(() => fieldCollection),
listCollections: () => listCollections,
};

field = await fieldFactory(db);
Expand Down
25 changes: 24 additions & 1 deletion src/api/models/publishedDataset.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,12 @@ describe('publishedDataset', () => {
createIndex: jest.fn(),
findOneAndUpdate: jest.fn(),
};
const listCollections = {
toArray: () => [true],
};
const db = {
collection: () => collection,
listCollections: () => listCollections,
};
let publishedDatasetCollection;

Expand Down Expand Up @@ -59,8 +63,12 @@ describe('publishedDataset', () => {
updateOne: jest.fn(),
createIndex: jest.fn(),
};
const listCollections = {
toArray: () => [true],
};
const db = {
collection: () => collection,
listCollections: () => listCollections,
};

let publishedDatasetCollection;
Expand Down Expand Up @@ -174,8 +182,12 @@ describe('publishedDataset', () => {
updateOne: jest.fn(),
createIndex: jest.fn(),
};
const listCollections = {
toArray: () => [true],
};
const db = {
collection: () => collection,
listCollections: () => listCollections,
};

let publishedDatasetCollection;
Expand Down Expand Up @@ -235,11 +247,15 @@ describe('publishedDataset', () => {
skip,
count,
}));
const listCollections = {
toArray: () => [true],
};
const db = {
collection: () => ({
find,
createIndex: jest.fn(),
}),
listCollections: () => listCollections,
};

let publishedDatasetCollection;
Expand Down Expand Up @@ -300,12 +316,15 @@ describe('publishedDataset', () => {
skip: skipToEmpty,
count,
}));

const listCollections = {
toArray: () => [true],
};
publishedDatasetCollection = await publishedDataset({
collection: () => ({
find: emptyFind,
createIndex: jest.fn(),
}),
listCollections: () => listCollections,
});

await publishedDatasetCollection.findPage({
Expand Down Expand Up @@ -404,11 +423,15 @@ describe('publishedDataset', () => {

describe('create', () => {
const insertOne = jest.fn().mockImplementation(() => 'inserted');
const listCollections = {
toArray: () => [true],
};
const db = {
collection: () => ({
insertOne,
createIndex: jest.fn(),
}),
listCollections: () => listCollections,
};

let publishedDatasetCollection;
Expand Down
4 changes: 4 additions & 0 deletions src/api/services/repositoryMiddleware.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ import { mongoClientFactory } from './repositoryMiddleware';

describe('mongoClient middleware', () => {
it('it should add db and collections to ctx', async () => {
const listCollections = {
toArray: () => [true],
};
const db = {
collection: () => ({ createIndex: () => {} }),
listCollections: () => listCollections,
};

const next = () => Promise.resolve();
Expand Down

0 comments on commit 3bbd406

Please sign in to comment.