Skip to content

Commit

Permalink
Lint tests too
Browse files Browse the repository at this point in the history
  • Loading branch information
pozylon committed Oct 22, 2024
1 parent f385700 commit 7e4b5f5
Show file tree
Hide file tree
Showing 89 changed files with 3,369 additions and 4,888 deletions.
6 changes: 1 addition & 5 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
**/lib
**/*.tests.js
**/*.test.js
**/*.tests.ts
**/*.test.ts
**/lib
2 changes: 1 addition & 1 deletion Jenkinsfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pipeline {
// sh 'cp ${DOTENV_PATH} ./env'
docker.build("ci:latest")
sh 'docker run ci:latest npm run lint:ci'
// sh 'docker run ci:latest npm run test:unit'
sh 'docker run ci:latest npm run test:unit'
sh 'docker run ci:latest npm run test:integration'
}
}
Expand Down
68 changes: 35 additions & 33 deletions packages/api/tests/api-index.test.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
// Import the function to be tested.
import {admin} from '../lib/roles/admin.js'
import {actions} from '../lib/roles/index.js';
import {checkAction, ensureActionExists, ensureIsFunction} from '../lib/acl.js'
import { admin } from '../lib/roles/admin.js';
import { actions } from '../lib/roles/index.js';
import { checkAction, ensureActionExists, ensureIsFunction } from '../lib/acl.js';
import { NoPermissionError, PermissionSystemError } from '../lib/errors.js';
import { Roles } from '@unchainedshop/roles';

describe('API', () => {

describe('roles', () => {
const role = {
allow: import.meta.jest.fn()
allow: import.meta.jest.fn(),
};

it('creates the admin role and grants permissions to all actions', () => {
admin(role, actions);
for (const actionName of Object.keys(actions)) {
expect(role.allow).toHaveBeenCalledWith(actions[actionName], expect.any(Function));
}
})

})
});
});

describe('ensureActionExists', () => {
it('should throw a PermissionSystemError if the action is undefined', () => {
expect(() => ensureActionExists(undefined, {})).toThrow(PermissionSystemError);
});

it('should not throw an error if the action is defined', () => {
expect(() => ensureActionExists('some action', {})).not.toThrow();
});
Expand All @@ -38,40 +36,44 @@ describe('API', () => {
const key = 'some key';
expect(() => ensureIsFunction(null, action, options, key)).toThrow(PermissionSystemError);
});

it('should not throw an error if the provided value is a function', () => {
const action = 'some action';
const options = { showKey: true };
const key = 'some key';
expect(() => ensureIsFunction(() => {}, action, options, key)).not.toThrow();
expect(() =>
ensureIsFunction(
() => {
/**/
},
action,
options,
key,
),
).not.toThrow();
});
});


describe('checkAction', () => {

it('should throw a NoPermissionError if the user does not have permission to perform the action', async () => {
Roles.userHasPermission = import.meta.jest.fn(async () => false);
it('should throw a NoPermissionError if the user does not have permission to perform the action', async () => {
Roles.userHasPermission = import.meta.jest.fn(async () => false);

const context = { userId: '123' };
const action = 'some action';
const args: any = [];
const options = { key: 'some key' };

const context = { userId: '123' };
const action = 'some action';
const args: any = [];
const options = { key: 'some key' };
return expect(checkAction(context, action, args, options)).rejects.toThrow(NoPermissionError);
});

await expect(checkAction(context, action, args, options)).rejects.toThrow(NoPermissionError);
});

it('should not throw an error if the user has permission to perform the action', async () => {
Roles.userHasPermission = import.meta.jest.fn(async () => true);
const context = { userId: '123' };
const action = 'some action';
const args: any = {};
const options = { key: 'some key' };
it('should not throw an error if the user has permission to perform the action', async () => {
Roles.userHasPermission = import.meta.jest.fn(async () => true);
const context = { userId: '123' };
const action = 'some action';
const args: any = {};
const options = { key: 'some key' };

await expect(checkAction(context, action, args, options)).resolves.not.toThrow();
return expect(checkAction(context, action, args, options)).resolves.not.toThrow();
});
});
});



});
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { Tree } from '@unchainedshop/utils';
import { concatItemsByLevels, divideTreeByLevels, fillToSameLengthArray, fillUp } from './zipTreeByDeepness.js';

import {
concatItemsByLevels,
divideTreeByLevels,
fillToSameLengthArray,
fillUp,
} from './zipTreeByDeepness.js';

describe('divideTreeByLevels', () => {
it('should return the expected result', () => {
Expand Down Expand Up @@ -49,9 +53,7 @@ describe('fillToSameLengthArray', () => {
});
});



describe("concatItemsByLevels", () => {
describe('concatItemsByLevels', () => {
it('should concatenate the array and create array with length 2', () => {
const levelArray = [
{ level: 1, items: ['a', 'b'] },
Expand All @@ -61,6 +63,4 @@ describe("concatItemsByLevels", () => {
const result = concatItemsByLevels(levelArray);
expect(result.length).toBe(2);
});


})
});
41 changes: 22 additions & 19 deletions packages/core-enrollments/src/module/buildFindSelector.test.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
import { EnrollmentStatus } from "@unchainedshop/core-enrollments";
import { buildFindSelector } from "./configureEnrollmentsModule.js";
import { EnrollmentStatus } from '@unchainedshop/core-enrollments';
import { buildFindSelector } from './configureEnrollmentsModule.js';

describe('buildFindSelector', () => {
it('Should correct filter when passed status, userId and queryString', () => {
expect(buildFindSelector({queryString: "Hello World", status: [EnrollmentStatus.ACTIVE], userId: 'admin-id'})).toEqual({
expect(
buildFindSelector({
queryString: 'Hello World',
status: [EnrollmentStatus.ACTIVE],
userId: 'admin-id',
}),
).toEqual({
deleted: null,
status: { '$in': [ 'ACTIVE' ] },
status: { $in: ['ACTIVE'] },
userId: 'admin-id',
'$text': { '$search': 'Hello World' }
})
$text: { $search: 'Hello World' },
});
});
it('Should correct filter when passed userId and queryString', () => {

expect(buildFindSelector({queryString: "Hello World", userId: 'admin-id'})).toEqual({
expect(buildFindSelector({ queryString: 'Hello World', userId: 'admin-id' })).toEqual({
deleted: null,
userId: 'admin-id',
'$text': { '$search': 'Hello World' }
})
$text: { $search: 'Hello World' },
});
});

it('Should correct filter when passed queryString', () => {

expect(buildFindSelector({queryString: "Hello World"})).toEqual({
deleted: null,
'$text': { '$search': 'Hello World' }
})
expect(buildFindSelector({ queryString: 'Hello World' })).toEqual({
deleted: null,
$text: { $search: 'Hello World' },
});
});

it('Should correct filter when passed no argument', () => {

expect(buildFindSelector({})).toEqual({
deleted: null,
})
deleted: null,
});
});
})
});
38 changes: 23 additions & 15 deletions packages/core-events/src/module/buildFindSelector.test.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
import { buildFindSelector } from "./configureEventsModule.js";
import { buildFindSelector } from './configureEventsModule.js';

describe('buildFindSelector', () => {
it('Return correct filter object when passed create, queryString, types', () => {
expect(buildFindSelector({created: new Date("2022-12-03T18:23:38.278Z"), queryString: "Hello world", types: ['PRODUCT_CREATED']})).toEqual({
type: { '$in': [ 'PRODUCT_CREATED' ] },
'$text': { '$search': 'Hello world' },
created: { '$gte': new Date( "2022-12-03T18:23:38.278Z" )}
})
expect(
buildFindSelector({
created: new Date('2022-12-03T18:23:38.278Z'),
queryString: 'Hello world',
types: ['PRODUCT_CREATED'],
}),
).toEqual({
type: { $in: ['PRODUCT_CREATED'] },
$text: { $search: 'Hello world' },
created: { $gte: new Date('2022-12-03T18:23:38.278Z') },
});
});

it('Return correct filter object when passed create, queryString', () => {
expect(buildFindSelector({created: new Date("2022-12-03T18:23:38.278Z"), queryString: "Hello world"})).toEqual({
'$text': { '$search': 'Hello world' },
created: { '$gte': new Date( "2022-12-03T18:23:38.278Z" )}
})
expect(
buildFindSelector({ created: new Date('2022-12-03T18:23:38.278Z'), queryString: 'Hello world' }),
).toEqual({
$text: { $search: 'Hello world' },
created: { $gte: new Date('2022-12-03T18:23:38.278Z') },
});
});

it('Return correct filter object when passed create', () => {
expect(buildFindSelector({created: new Date("2022-12-03T18:23:38.278Z")})).toEqual({
created: { '$gte': new Date( "2022-12-03T18:23:38.278Z" )}
})
expect(buildFindSelector({ created: new Date('2022-12-03T18:23:38.278Z') })).toEqual({
created: { $gte: new Date('2022-12-03T18:23:38.278Z') },
});
});

it('Return correct filter object when passed no argument', () => {
expect(buildFindSelector({})).toEqual({})
expect(buildFindSelector({})).toEqual({});
});
})
});
4 changes: 2 additions & 2 deletions packages/core-filters/src/filter-value-parsers/range.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import range from "./range.js";
import range from './range.js';

describe('range', () => {
it('returns an empty array if no range is provided', () => {
Expand All @@ -21,4 +21,4 @@ describe('range', () => {
const result = range(values, allKeys);
expect(result).toEqual(['1', '2', '3']);
});
});
});
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import { defaultSelector, resolveAssortmentSelector } from "./resolveAssortmentSelector.js";



import { defaultSelector, resolveAssortmentSelector } from './resolveAssortmentSelector.js';

describe('defaultSelector', () => {
it('returns an object with isActive key set to true if no query is provided', () => {
const result = defaultSelector();
expect(result).toEqual({isActive: true});
expect(result).toEqual({ isActive: true });
});

it('returns an object with the isActive property set to true if the includeInactive property is not provided or is false', () => {
Expand All @@ -26,7 +23,7 @@ describe('defaultSelector', () => {
describe('resolveAssortmentSelector', () => {
it('returns an object with isActive key set to true if no query is provided', () => {
const result = resolveAssortmentSelector();
expect(result).toEqual({isActive: true});
expect(result).toEqual({ isActive: true });
});

it('returns an object with the isActive property set to true if the includeInactive property is not provided or is false', () => {
Expand All @@ -42,4 +39,3 @@ describe('resolveAssortmentSelector', () => {
expect(result).toEqual({});
});
});

62 changes: 31 additions & 31 deletions packages/core-filters/src/utils/intersectSet.test.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
import { intersectSet } from "./intersectSet.js";
import { intersectSet } from './intersectSet.js';

describe('intersectSet', () => {
it('should return an empty set if the two input sets are empty', () => {
const productIdSet: Set<string> = new Set();
const filterProductIdSet: Set<string> = new Set();
const result = intersectSet(productIdSet, filterProductIdSet);
expect(result).toEqual(new Set());
});
it('should return an empty set if the two input sets do not have any common elements', () => {
const productIdSet = new Set(['1', '2', '3']);
const filterProductIdSet = new Set(['4', '5', '6']);
const result = intersectSet(productIdSet, filterProductIdSet);
expect(result).toEqual(new Set());
});
it('should return a set containing only the common elements of the two input sets', () => {
const productIdSet = new Set(['1', '2', '3', '4']);
const filterProductIdSet = new Set(['2', '3', '5', '6']);
const result = intersectSet(productIdSet, filterProductIdSet);
expect(result).toEqual(new Set(['2', '3']));
});
it('should not modify the original input sets', () => {
const productIdSet = new Set(['1', '2', '3']);
const filterProductIdSet = new Set(['2', '3', '4', '5']);
intersectSet(productIdSet, filterProductIdSet);
expect(productIdSet).toEqual(new Set(['1', '2', '3']));
expect(filterProductIdSet).toEqual(new Set(['2', '3', '4', '5']));
});
});
describe('intersectSet', () => {
it('should return an empty set if the two input sets are empty', () => {
const productIdSet: Set<string> = new Set();
const filterProductIdSet: Set<string> = new Set();
const result = intersectSet(productIdSet, filterProductIdSet);
expect(result).toEqual(new Set());
});

it('should return an empty set if the two input sets do not have any common elements', () => {
const productIdSet = new Set(['1', '2', '3']);
const filterProductIdSet = new Set(['4', '5', '6']);
const result = intersectSet(productIdSet, filterProductIdSet);
expect(result).toEqual(new Set());
});

it('should return a set containing only the common elements of the two input sets', () => {
const productIdSet = new Set(['1', '2', '3', '4']);
const filterProductIdSet = new Set(['2', '3', '5', '6']);
const result = intersectSet(productIdSet, filterProductIdSet);
expect(result).toEqual(new Set(['2', '3']));
});

it('should not modify the original input sets', () => {
const productIdSet = new Set(['1', '2', '3']);
const filterProductIdSet = new Set(['2', '3', '4', '5']);
intersectSet(productIdSet, filterProductIdSet);
expect(productIdSet).toEqual(new Set(['1', '2', '3']));
expect(filterProductIdSet).toEqual(new Set(['2', '3', '4', '5']));
});
});
Loading

0 comments on commit 7e4b5f5

Please sign in to comment.