mirror of
https://github.com/strapi/strapi.git
synced 2025-09-17 12:27:33 +00:00
Migrate missing tests to ts
This commit is contained in:
parent
646a1f0b83
commit
1cefae94ca
@ -1,245 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
||||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
||||||
};
|
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
|
||||||
const content_api_1 = __importDefault(require("../content-api"));
|
|
||||||
describe('Content API - Permissions', () => {
|
|
||||||
const bindToContentAPI = (action) => {
|
|
||||||
Object.assign(action, { [Symbol.for('__type__')]: ['content-api'] });
|
|
||||||
return action;
|
|
||||||
};
|
|
||||||
describe('Get Actions Map', () => {
|
|
||||||
test('When no API are defined, it should return an empty object', () => {
|
|
||||||
global.strapi = {};
|
|
||||||
const contentAPI = (0, content_api_1.default)(global.strapi);
|
|
||||||
const actionsMap = contentAPI.permissions.getActionsMap();
|
|
||||||
expect(actionsMap).toEqual({});
|
|
||||||
});
|
|
||||||
test('When no controller are defined for an API, it should ignore the API', () => {
|
|
||||||
global.strapi = {
|
|
||||||
api: {
|
|
||||||
foo: {},
|
|
||||||
bar: {},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
const contentAPI = (0, content_api_1.default)(global.strapi);
|
|
||||||
const actionsMap = contentAPI.permissions.getActionsMap();
|
|
||||||
expect(actionsMap).toEqual({});
|
|
||||||
});
|
|
||||||
test(`Do not register controller if they're not bound to the content API`, () => {
|
|
||||||
const actionC = () => { };
|
|
||||||
Object.assign(actionC, { [Symbol.for('__type__')]: ['admin-api'] });
|
|
||||||
global.strapi = {
|
|
||||||
api: {
|
|
||||||
foo: {
|
|
||||||
controllers: {
|
|
||||||
controllerA: {
|
|
||||||
actionA: bindToContentAPI(() => { }),
|
|
||||||
actionB() { },
|
|
||||||
actionC,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
const contentAPI = (0, content_api_1.default)(global.strapi);
|
|
||||||
const actionsMap = contentAPI.permissions.getActionsMap();
|
|
||||||
expect(actionsMap).toEqual({
|
|
||||||
'api::foo': { controllers: { controllerA: ['actionA'] } },
|
|
||||||
});
|
|
||||||
});
|
|
||||||
test('Creates and populate a map of actions from APIs and plugins', () => {
|
|
||||||
global.strapi = {
|
|
||||||
api: {
|
|
||||||
foo: {
|
|
||||||
controllers: {
|
|
||||||
controllerA: {
|
|
||||||
actionA: bindToContentAPI(() => { }),
|
|
||||||
actionB: bindToContentAPI(() => { }),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
bar: {
|
|
||||||
controllers: {
|
|
||||||
controllerA: {
|
|
||||||
actionA: bindToContentAPI(() => { }),
|
|
||||||
actionB: bindToContentAPI(() => { }),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
foobar: {
|
|
||||||
controllers: {
|
|
||||||
controllerA: {
|
|
||||||
actionA: bindToContentAPI(() => { }),
|
|
||||||
actionB: bindToContentAPI(() => { }),
|
|
||||||
},
|
|
||||||
controllerB: {
|
|
||||||
actionC: bindToContentAPI(() => { }),
|
|
||||||
actionD: bindToContentAPI(() => { }),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
plugins: {
|
|
||||||
foo: {
|
|
||||||
controllers: {
|
|
||||||
controllerA: {
|
|
||||||
actionA: bindToContentAPI(() => { }),
|
|
||||||
actionB: bindToContentAPI(() => { }),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
bar: {
|
|
||||||
controllers: {
|
|
||||||
controllerA: {
|
|
||||||
actionA: bindToContentAPI(() => { }),
|
|
||||||
actionB: bindToContentAPI(() => { }),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
const contentAPI = (0, content_api_1.default)(global.strapi);
|
|
||||||
const actionsMap = contentAPI.permissions.getActionsMap();
|
|
||||||
expect(actionsMap).toEqual({
|
|
||||||
'api::foo': { controllers: { controllerA: ['actionA', 'actionB'] } },
|
|
||||||
'api::bar': { controllers: { controllerA: ['actionA', 'actionB'] } },
|
|
||||||
'api::foobar': {
|
|
||||||
controllers: {
|
|
||||||
controllerA: ['actionA', 'actionB'],
|
|
||||||
controllerB: ['actionC', 'actionD'],
|
|
||||||
},
|
|
||||||
},
|
|
||||||
'plugin::foo': { controllers: { controllerA: ['actionA', 'actionB'] } },
|
|
||||||
'plugin::bar': { controllers: { controllerA: ['actionA', 'actionB'] } },
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
describe('Register Actions', () => {
|
|
||||||
beforeEach(() => {
|
|
||||||
global.strapi = {
|
|
||||||
api: {
|
|
||||||
foo: {
|
|
||||||
controllers: {
|
|
||||||
controllerA: {
|
|
||||||
actionA: bindToContentAPI(() => { }),
|
|
||||||
actionB: bindToContentAPI(() => { }),
|
|
||||||
},
|
|
||||||
controllerB: {
|
|
||||||
actionC: bindToContentAPI(() => { }),
|
|
||||||
actionD: bindToContentAPI(() => { }),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
plugins: {
|
|
||||||
foo: {
|
|
||||||
controllers: {
|
|
||||||
controllerA: {
|
|
||||||
actionA: bindToContentAPI(() => { }),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
});
|
|
||||||
test('The action provider should holds every action from APIs and plugins', async () => {
|
|
||||||
const contentAPI = (0, content_api_1.default)(global.strapi);
|
|
||||||
await contentAPI.permissions.registerActions();
|
|
||||||
const values = contentAPI.permissions.providers.action.values();
|
|
||||||
expect(values).toEqual([
|
|
||||||
{
|
|
||||||
uid: 'api::foo.controllerA.actionA',
|
|
||||||
api: 'api::foo',
|
|
||||||
controller: 'controllerA',
|
|
||||||
action: 'actionA',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
uid: 'api::foo.controllerA.actionB',
|
|
||||||
api: 'api::foo',
|
|
||||||
controller: 'controllerA',
|
|
||||||
action: 'actionB',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
uid: 'api::foo.controllerB.actionC',
|
|
||||||
api: 'api::foo',
|
|
||||||
controller: 'controllerB',
|
|
||||||
action: 'actionC',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
uid: 'api::foo.controllerB.actionD',
|
|
||||||
api: 'api::foo',
|
|
||||||
controller: 'controllerB',
|
|
||||||
action: 'actionD',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
uid: 'plugin::foo.controllerA.actionA',
|
|
||||||
api: 'plugin::foo',
|
|
||||||
controller: 'controllerA',
|
|
||||||
action: 'actionA',
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
});
|
|
||||||
test('Call registerActions twice should throw a duplicate error', async () => {
|
|
||||||
const contentAPI = (0, content_api_1.default)(global.strapi);
|
|
||||||
await contentAPI.permissions.registerActions();
|
|
||||||
expect(() => contentAPI.permissions.registerActions()).rejects.toThrowError('Duplicated item key: api::foo.controllerA.actionA');
|
|
||||||
});
|
|
||||||
});
|
|
||||||
describe('Providers', () => {
|
|
||||||
test('You should not be able to register action once strapi is loaded', () => {
|
|
||||||
global.strapi.isLoaded = true;
|
|
||||||
const contentAPI = (0, content_api_1.default)(global.strapi);
|
|
||||||
// Actions
|
|
||||||
expect(() => contentAPI.permissions.providers.action.register('foo', {})).rejects.toThrowError(`You can't register new actions outside the bootstrap function.`);
|
|
||||||
// Conditions
|
|
||||||
expect(() => contentAPI.permissions.providers.condition.register({ name: 'myCondition' })).rejects.toThrowError(`You can't register new conditions outside the bootstrap function.`);
|
|
||||||
// Register Actions
|
|
||||||
expect(() => contentAPI.permissions.registerActions()).rejects.toThrowError(`You can't register new actions outside the bootstrap function.`);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
describe('Engine', () => {
|
|
||||||
test('Engine warns when registering an unknown action', async () => {
|
|
||||||
global.strapi = {
|
|
||||||
log: {
|
|
||||||
debug: jest.fn(),
|
|
||||||
},
|
|
||||||
};
|
|
||||||
const contentAPI = (0, content_api_1.default)();
|
|
||||||
const ability = await contentAPI.permissions.engine.generateAbility([{ action: 'foo' }]);
|
|
||||||
expect(ability.rules).toHaveLength(0);
|
|
||||||
expect(global.strapi.log.debug).toHaveBeenCalledWith(`Unknown action "foo" supplied when registering a new permission`);
|
|
||||||
});
|
|
||||||
test('Engine filter out invalid action when generating an ability', async () => {
|
|
||||||
global.strapi = {
|
|
||||||
log: {
|
|
||||||
debug: jest.fn(),
|
|
||||||
},
|
|
||||||
api: {
|
|
||||||
foo: {
|
|
||||||
controllers: {
|
|
||||||
bar: { foobar: bindToContentAPI(() => { }) },
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
};
|
|
||||||
const contentAPI = (0, content_api_1.default)(global.strapi);
|
|
||||||
await contentAPI.permissions.registerActions();
|
|
||||||
const ability = await contentAPI.permissions.engine.generateAbility([
|
|
||||||
{ action: 'foo' },
|
|
||||||
{ action: 'api::foo.bar.foobar' },
|
|
||||||
]);
|
|
||||||
expect(ability.rules).toHaveLength(1);
|
|
||||||
expect(ability.rules).toEqual([
|
|
||||||
{
|
|
||||||
action: 'api::foo.bar.foobar',
|
|
||||||
subject: 'all',
|
|
||||||
},
|
|
||||||
]);
|
|
||||||
expect(global.strapi.log.debug).toHaveBeenCalledTimes(1);
|
|
||||||
expect(global.strapi.log.debug).toHaveBeenCalledWith(`Unknown action "foo" supplied when registering a new permission`);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
//# sourceMappingURL=content-api-permissions.test.js.map
|
|
@ -0,0 +1,290 @@
|
|||||||
|
import createContentAPI from '../content-api';
|
||||||
|
import type { Utils } from '../..';
|
||||||
|
|
||||||
|
describe('Content API - Permissions', () => {
|
||||||
|
const bindToContentAPI = (action: Utils.Function.Any) => {
|
||||||
|
Object.assign(action, { [Symbol.for('__type__')]: ['content-api'] });
|
||||||
|
return action;
|
||||||
|
};
|
||||||
|
|
||||||
|
describe('Get Actions Map', () => {
|
||||||
|
test('When no API are defined, it should return an empty object', () => {
|
||||||
|
global.strapi = {} as any;
|
||||||
|
|
||||||
|
const contentAPI = createContentAPI(global.strapi);
|
||||||
|
const actionsMap = contentAPI.permissions.getActionsMap();
|
||||||
|
|
||||||
|
expect(actionsMap).toEqual({});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('When no controller are defined for an API, it should ignore the API', () => {
|
||||||
|
global.strapi = {
|
||||||
|
api: {
|
||||||
|
foo: {},
|
||||||
|
bar: {},
|
||||||
|
},
|
||||||
|
} as any;
|
||||||
|
|
||||||
|
const contentAPI = createContentAPI(global.strapi);
|
||||||
|
const actionsMap = contentAPI.permissions.getActionsMap();
|
||||||
|
|
||||||
|
expect(actionsMap).toEqual({});
|
||||||
|
});
|
||||||
|
|
||||||
|
test(`Do not register controller if they're not bound to the content API`, () => {
|
||||||
|
const actionC = () => {};
|
||||||
|
Object.assign(actionC, { [Symbol.for('__type__')]: ['admin-api'] });
|
||||||
|
|
||||||
|
global.strapi = {
|
||||||
|
api: {
|
||||||
|
foo: {
|
||||||
|
controllers: {
|
||||||
|
controllerA: {
|
||||||
|
actionA: bindToContentAPI(() => {}),
|
||||||
|
actionB() {},
|
||||||
|
actionC,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as any;
|
||||||
|
|
||||||
|
const contentAPI = createContentAPI(global.strapi);
|
||||||
|
const actionsMap = contentAPI.permissions.getActionsMap();
|
||||||
|
|
||||||
|
expect(actionsMap).toEqual({
|
||||||
|
'api::foo': { controllers: { controllerA: ['actionA'] } },
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Creates and populate a map of actions from APIs and plugins', () => {
|
||||||
|
global.strapi = {
|
||||||
|
api: {
|
||||||
|
foo: {
|
||||||
|
controllers: {
|
||||||
|
controllerA: {
|
||||||
|
actionA: bindToContentAPI(() => {}),
|
||||||
|
actionB: bindToContentAPI(() => {}),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
bar: {
|
||||||
|
controllers: {
|
||||||
|
controllerA: {
|
||||||
|
actionA: bindToContentAPI(() => {}),
|
||||||
|
actionB: bindToContentAPI(() => {}),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
foobar: {
|
||||||
|
controllers: {
|
||||||
|
controllerA: {
|
||||||
|
actionA: bindToContentAPI(() => {}),
|
||||||
|
actionB: bindToContentAPI(() => {}),
|
||||||
|
},
|
||||||
|
controllerB: {
|
||||||
|
actionC: bindToContentAPI(() => {}),
|
||||||
|
actionD: bindToContentAPI(() => {}),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
plugins: {
|
||||||
|
foo: {
|
||||||
|
controllers: {
|
||||||
|
controllerA: {
|
||||||
|
actionA: bindToContentAPI(() => {}),
|
||||||
|
actionB: bindToContentAPI(() => {}),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
bar: {
|
||||||
|
controllers: {
|
||||||
|
controllerA: {
|
||||||
|
actionA: bindToContentAPI(() => {}),
|
||||||
|
actionB: bindToContentAPI(() => {}),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as any;
|
||||||
|
|
||||||
|
const contentAPI = createContentAPI(global.strapi);
|
||||||
|
const actionsMap = contentAPI.permissions.getActionsMap();
|
||||||
|
|
||||||
|
expect(actionsMap).toEqual({
|
||||||
|
'api::foo': { controllers: { controllerA: ['actionA', 'actionB'] } },
|
||||||
|
'api::bar': { controllers: { controllerA: ['actionA', 'actionB'] } },
|
||||||
|
'api::foobar': {
|
||||||
|
controllers: {
|
||||||
|
controllerA: ['actionA', 'actionB'],
|
||||||
|
controllerB: ['actionC', 'actionD'],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
'plugin::foo': { controllers: { controllerA: ['actionA', 'actionB'] } },
|
||||||
|
'plugin::bar': { controllers: { controllerA: ['actionA', 'actionB'] } },
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Register Actions', () => {
|
||||||
|
beforeEach(() => {
|
||||||
|
global.strapi = {
|
||||||
|
api: {
|
||||||
|
foo: {
|
||||||
|
controllers: {
|
||||||
|
controllerA: {
|
||||||
|
actionA: bindToContentAPI(() => {}),
|
||||||
|
actionB: bindToContentAPI(() => {}),
|
||||||
|
},
|
||||||
|
controllerB: {
|
||||||
|
actionC: bindToContentAPI(() => {}),
|
||||||
|
actionD: bindToContentAPI(() => {}),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
plugins: {
|
||||||
|
foo: {
|
||||||
|
controllers: {
|
||||||
|
controllerA: {
|
||||||
|
actionA: bindToContentAPI(() => {}),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as any;
|
||||||
|
});
|
||||||
|
|
||||||
|
test('The action provider should holds every action from APIs and plugins', async () => {
|
||||||
|
const contentAPI = createContentAPI(global.strapi);
|
||||||
|
|
||||||
|
await contentAPI.permissions.registerActions();
|
||||||
|
|
||||||
|
const values = contentAPI.permissions.providers.action.values();
|
||||||
|
|
||||||
|
expect(values).toEqual([
|
||||||
|
{
|
||||||
|
uid: 'api::foo.controllerA.actionA',
|
||||||
|
api: 'api::foo',
|
||||||
|
controller: 'controllerA',
|
||||||
|
action: 'actionA',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
uid: 'api::foo.controllerA.actionB',
|
||||||
|
api: 'api::foo',
|
||||||
|
controller: 'controllerA',
|
||||||
|
action: 'actionB',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
uid: 'api::foo.controllerB.actionC',
|
||||||
|
api: 'api::foo',
|
||||||
|
controller: 'controllerB',
|
||||||
|
action: 'actionC',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
uid: 'api::foo.controllerB.actionD',
|
||||||
|
api: 'api::foo',
|
||||||
|
controller: 'controllerB',
|
||||||
|
action: 'actionD',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
uid: 'plugin::foo.controllerA.actionA',
|
||||||
|
api: 'plugin::foo',
|
||||||
|
controller: 'controllerA',
|
||||||
|
action: 'actionA',
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Call registerActions twice should throw a duplicate error', async () => {
|
||||||
|
const contentAPI = createContentAPI(global.strapi);
|
||||||
|
|
||||||
|
await contentAPI.permissions.registerActions();
|
||||||
|
|
||||||
|
expect(() => contentAPI.permissions.registerActions()).rejects.toThrowError(
|
||||||
|
'Duplicated item key: api::foo.controllerA.actionA'
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Providers', () => {
|
||||||
|
test('You should not be able to register action once strapi is loaded', () => {
|
||||||
|
global.strapi.isLoaded = true;
|
||||||
|
|
||||||
|
const contentAPI = createContentAPI(global.strapi);
|
||||||
|
|
||||||
|
// Actions
|
||||||
|
expect(() =>
|
||||||
|
contentAPI.permissions.providers.action.register('foo', {})
|
||||||
|
).rejects.toThrowError(`You can't register new actions outside the bootstrap function.`);
|
||||||
|
|
||||||
|
// Conditions
|
||||||
|
expect(() =>
|
||||||
|
contentAPI.permissions.providers.condition.register({ name: 'myCondition' })
|
||||||
|
).rejects.toThrowError(`You can't register new conditions outside the bootstrap function.`);
|
||||||
|
|
||||||
|
// Register Actions
|
||||||
|
expect(() => contentAPI.permissions.registerActions()).rejects.toThrowError(
|
||||||
|
`You can't register new actions outside the bootstrap function.`
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Engine', () => {
|
||||||
|
test('Engine warns when registering an unknown action', async () => {
|
||||||
|
global.strapi = {
|
||||||
|
log: {
|
||||||
|
debug: jest.fn(),
|
||||||
|
},
|
||||||
|
} as any;
|
||||||
|
|
||||||
|
const contentAPI = createContentAPI(global.strapi);
|
||||||
|
|
||||||
|
const ability = await contentAPI.permissions.engine.generateAbility([{ action: 'foo' }]);
|
||||||
|
|
||||||
|
expect(ability.rules).toHaveLength(0);
|
||||||
|
expect(global.strapi.log.debug).toHaveBeenCalledWith(
|
||||||
|
`Unknown action "foo" supplied when registering a new permission`
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Engine filter out invalid action when generating an ability', async () => {
|
||||||
|
global.strapi = {
|
||||||
|
log: {
|
||||||
|
debug: jest.fn(),
|
||||||
|
},
|
||||||
|
|
||||||
|
api: {
|
||||||
|
foo: {
|
||||||
|
controllers: {
|
||||||
|
bar: { foobar: bindToContentAPI(() => {}) },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
} as any;
|
||||||
|
|
||||||
|
const contentAPI = createContentAPI(global.strapi);
|
||||||
|
|
||||||
|
await contentAPI.permissions.registerActions();
|
||||||
|
|
||||||
|
const ability = await contentAPI.permissions.engine.generateAbility([
|
||||||
|
{ action: 'foo' },
|
||||||
|
{ action: 'api::foo.bar.foobar' },
|
||||||
|
]);
|
||||||
|
|
||||||
|
expect(ability.rules).toHaveLength(1);
|
||||||
|
expect(ability.rules).toEqual([
|
||||||
|
{
|
||||||
|
action: 'api::foo.bar.foobar',
|
||||||
|
subject: 'all',
|
||||||
|
},
|
||||||
|
]);
|
||||||
|
|
||||||
|
expect(global.strapi.log.debug).toHaveBeenCalledTimes(1);
|
||||||
|
expect(global.strapi.log.debug).toHaveBeenCalledWith(
|
||||||
|
`Unknown action "foo" supplied when registering a new permission`
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
@ -1,122 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
|
||||||
const core_store_1 = require("../core-store");
|
|
||||||
const data = {
|
|
||||||
key: 'testKey',
|
|
||||||
value: 'testValue',
|
|
||||||
};
|
|
||||||
const queryRes = {
|
|
||||||
id: 1,
|
|
||||||
key: 'testKey',
|
|
||||||
value: 'testValue',
|
|
||||||
type: 'string',
|
|
||||||
environment: null,
|
|
||||||
tag: null,
|
|
||||||
};
|
|
||||||
const where = {
|
|
||||||
key: 'plugin_testName_testKey',
|
|
||||||
environment: 'test',
|
|
||||||
tag: null,
|
|
||||||
};
|
|
||||||
describe('Core Store', () => {
|
|
||||||
test('Set key in empty store', async () => {
|
|
||||||
const fakeEmptyDBQuery = {
|
|
||||||
findOne: jest.fn(() => Promise.resolve()),
|
|
||||||
update: jest.fn(() => Promise.resolve(queryRes)),
|
|
||||||
create: jest.fn(() => Promise.resolve(queryRes)),
|
|
||||||
delete: jest.fn(() => Promise.resolve()),
|
|
||||||
};
|
|
||||||
const fakeEmptyDB = {
|
|
||||||
query: jest.fn(() => fakeEmptyDBQuery),
|
|
||||||
};
|
|
||||||
const emptyTestStore = (0, core_store_1.createCoreStore)({
|
|
||||||
db: fakeEmptyDB,
|
|
||||||
});
|
|
||||||
const store = emptyTestStore({
|
|
||||||
environment: 'test',
|
|
||||||
type: 'plugin',
|
|
||||||
name: 'testName',
|
|
||||||
});
|
|
||||||
await store.set(data);
|
|
||||||
expect(fakeEmptyDB.query).toHaveBeenCalledTimes(2);
|
|
||||||
expect(fakeEmptyDBQuery.findOne).toHaveBeenCalledWith({ where });
|
|
||||||
expect(fakeEmptyDBQuery.create).toHaveBeenCalledWith({
|
|
||||||
data: { ...where, value: JSON.stringify('testValue'), type: 'string' },
|
|
||||||
});
|
|
||||||
});
|
|
||||||
test('Set key in not empty store', async () => {
|
|
||||||
const fakeNotEmptyDBQuery = {
|
|
||||||
findOne: jest.fn(() => Promise.resolve(queryRes)),
|
|
||||||
update: jest.fn(() => Promise.resolve(queryRes)),
|
|
||||||
create: jest.fn(() => Promise.resolve(queryRes)),
|
|
||||||
delete: jest.fn(() => Promise.resolve()),
|
|
||||||
};
|
|
||||||
const fakeNotEmptyDB = {
|
|
||||||
query: jest.fn(() => fakeNotEmptyDBQuery),
|
|
||||||
};
|
|
||||||
const notEmptyTestStore = (0, core_store_1.createCoreStore)({
|
|
||||||
db: fakeNotEmptyDB,
|
|
||||||
});
|
|
||||||
const store = notEmptyTestStore({
|
|
||||||
environment: 'test',
|
|
||||||
type: 'plugin',
|
|
||||||
name: 'testName',
|
|
||||||
});
|
|
||||||
await store.set(data);
|
|
||||||
expect(fakeNotEmptyDB.query).toHaveBeenCalledTimes(2);
|
|
||||||
expect(fakeNotEmptyDBQuery.findOne).toHaveBeenCalledWith({ where });
|
|
||||||
expect(fakeNotEmptyDBQuery.update).toHaveBeenCalledWith({
|
|
||||||
where: { id: queryRes.id },
|
|
||||||
data: {
|
|
||||||
type: 'string',
|
|
||||||
value: JSON.stringify('testValue'),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
|
||||||
test('Delete key from empty store', async () => {
|
|
||||||
const fakeEmptyDBQuery = {
|
|
||||||
findOne: jest.fn(() => Promise.resolve()),
|
|
||||||
update: jest.fn(() => Promise.resolve(queryRes)),
|
|
||||||
create: jest.fn(() => Promise.resolve(queryRes)),
|
|
||||||
delete: jest.fn(() => Promise.resolve()),
|
|
||||||
};
|
|
||||||
const fakeEmptyDB = {
|
|
||||||
query: jest.fn(() => fakeEmptyDBQuery),
|
|
||||||
};
|
|
||||||
const emptyTestStore = (0, core_store_1.createCoreStore)({
|
|
||||||
db: fakeEmptyDB,
|
|
||||||
});
|
|
||||||
const store = emptyTestStore({
|
|
||||||
environment: 'test',
|
|
||||||
type: 'plugin',
|
|
||||||
name: 'testName',
|
|
||||||
});
|
|
||||||
await store.delete(data);
|
|
||||||
expect(fakeEmptyDB.query).toHaveBeenCalledTimes(1);
|
|
||||||
expect(fakeEmptyDBQuery.delete).toHaveBeenCalledWith({ where });
|
|
||||||
});
|
|
||||||
test('Delete key from not empty store', async () => {
|
|
||||||
const fakeNotEmptyDBQuery = {
|
|
||||||
findOne: jest.fn(() => Promise.resolve(queryRes)),
|
|
||||||
update: jest.fn(() => Promise.resolve(queryRes)),
|
|
||||||
create: jest.fn(() => Promise.resolve(queryRes)),
|
|
||||||
delete: jest.fn(() => Promise.resolve()),
|
|
||||||
};
|
|
||||||
const fakeNotEmptyDB = {
|
|
||||||
query: jest.fn(() => fakeNotEmptyDBQuery),
|
|
||||||
};
|
|
||||||
const notEmptyTestStore = (0, core_store_1.createCoreStore)({
|
|
||||||
db: fakeNotEmptyDB,
|
|
||||||
});
|
|
||||||
const store = notEmptyTestStore({
|
|
||||||
environment: 'test',
|
|
||||||
type: 'plugin',
|
|
||||||
name: 'testName',
|
|
||||||
});
|
|
||||||
const rest = await store.delete(data);
|
|
||||||
expect(fakeNotEmptyDB.query).toHaveBeenCalledTimes(1);
|
|
||||||
expect(fakeNotEmptyDBQuery.delete).toHaveBeenCalledWith({ where });
|
|
||||||
expect(rest).toEqual(undefined);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
//# sourceMappingURL=core-store.test.js.map
|
|
146
packages/core/strapi/src/services/__tests__/core-store.test.ts
Normal file
146
packages/core/strapi/src/services/__tests__/core-store.test.ts
Normal file
@ -0,0 +1,146 @@
|
|||||||
|
import { createCoreStore } from '../core-store';
|
||||||
|
|
||||||
|
const data = {
|
||||||
|
key: 'testKey',
|
||||||
|
value: 'testValue',
|
||||||
|
};
|
||||||
|
|
||||||
|
const queryRes = {
|
||||||
|
id: 1,
|
||||||
|
key: 'testKey',
|
||||||
|
value: 'testValue',
|
||||||
|
type: 'string',
|
||||||
|
environment: null,
|
||||||
|
tag: null,
|
||||||
|
};
|
||||||
|
|
||||||
|
const where = {
|
||||||
|
key: 'plugin_testName_testKey',
|
||||||
|
environment: 'test',
|
||||||
|
tag: null,
|
||||||
|
};
|
||||||
|
|
||||||
|
describe('Core Store', () => {
|
||||||
|
test('Set key in empty store', async () => {
|
||||||
|
const fakeEmptyDBQuery = {
|
||||||
|
findOne: jest.fn(() => Promise.resolve()),
|
||||||
|
update: jest.fn(() => Promise.resolve(queryRes)),
|
||||||
|
create: jest.fn(() => Promise.resolve(queryRes)),
|
||||||
|
delete: jest.fn(() => Promise.resolve()),
|
||||||
|
};
|
||||||
|
|
||||||
|
const fakeEmptyDB = {
|
||||||
|
query: jest.fn(() => fakeEmptyDBQuery),
|
||||||
|
};
|
||||||
|
|
||||||
|
const emptyTestStore = createCoreStore({
|
||||||
|
db: fakeEmptyDB,
|
||||||
|
});
|
||||||
|
|
||||||
|
const store = emptyTestStore({
|
||||||
|
environment: 'test',
|
||||||
|
type: 'plugin',
|
||||||
|
name: 'testName',
|
||||||
|
});
|
||||||
|
|
||||||
|
await store.set(data);
|
||||||
|
|
||||||
|
expect(fakeEmptyDB.query).toHaveBeenCalledTimes(2);
|
||||||
|
expect(fakeEmptyDBQuery.findOne).toHaveBeenCalledWith({ where });
|
||||||
|
expect(fakeEmptyDBQuery.create).toHaveBeenCalledWith({
|
||||||
|
data: { ...where, value: JSON.stringify('testValue'), type: 'string' },
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Set key in not empty store', async () => {
|
||||||
|
const fakeNotEmptyDBQuery = {
|
||||||
|
findOne: jest.fn(() => Promise.resolve(queryRes)),
|
||||||
|
update: jest.fn(() => Promise.resolve(queryRes)),
|
||||||
|
create: jest.fn(() => Promise.resolve(queryRes)),
|
||||||
|
delete: jest.fn(() => Promise.resolve()),
|
||||||
|
};
|
||||||
|
|
||||||
|
const fakeNotEmptyDB = {
|
||||||
|
query: jest.fn(() => fakeNotEmptyDBQuery),
|
||||||
|
};
|
||||||
|
|
||||||
|
const notEmptyTestStore = createCoreStore({
|
||||||
|
db: fakeNotEmptyDB,
|
||||||
|
});
|
||||||
|
|
||||||
|
const store = notEmptyTestStore({
|
||||||
|
environment: 'test',
|
||||||
|
type: 'plugin',
|
||||||
|
name: 'testName',
|
||||||
|
});
|
||||||
|
|
||||||
|
await store.set(data);
|
||||||
|
|
||||||
|
expect(fakeNotEmptyDB.query).toHaveBeenCalledTimes(2);
|
||||||
|
expect(fakeNotEmptyDBQuery.findOne).toHaveBeenCalledWith({ where });
|
||||||
|
expect(fakeNotEmptyDBQuery.update).toHaveBeenCalledWith({
|
||||||
|
where: { id: queryRes.id },
|
||||||
|
data: {
|
||||||
|
type: 'string',
|
||||||
|
value: JSON.stringify('testValue'),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Delete key from empty store', async () => {
|
||||||
|
const fakeEmptyDBQuery = {
|
||||||
|
findOne: jest.fn(() => Promise.resolve()),
|
||||||
|
update: jest.fn(() => Promise.resolve(queryRes)),
|
||||||
|
create: jest.fn(() => Promise.resolve(queryRes)),
|
||||||
|
delete: jest.fn(() => Promise.resolve()),
|
||||||
|
};
|
||||||
|
|
||||||
|
const fakeEmptyDB = {
|
||||||
|
query: jest.fn(() => fakeEmptyDBQuery),
|
||||||
|
};
|
||||||
|
|
||||||
|
const emptyTestStore = createCoreStore({
|
||||||
|
db: fakeEmptyDB,
|
||||||
|
});
|
||||||
|
|
||||||
|
const store = emptyTestStore({
|
||||||
|
environment: 'test',
|
||||||
|
type: 'plugin',
|
||||||
|
name: 'testName',
|
||||||
|
});
|
||||||
|
|
||||||
|
await store.delete(data);
|
||||||
|
|
||||||
|
expect(fakeEmptyDB.query).toHaveBeenCalledTimes(1);
|
||||||
|
expect(fakeEmptyDBQuery.delete).toHaveBeenCalledWith({ where });
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Delete key from not empty store', async () => {
|
||||||
|
const fakeNotEmptyDBQuery = {
|
||||||
|
findOne: jest.fn(() => Promise.resolve(queryRes)),
|
||||||
|
update: jest.fn(() => Promise.resolve(queryRes)),
|
||||||
|
create: jest.fn(() => Promise.resolve(queryRes)),
|
||||||
|
delete: jest.fn(() => Promise.resolve()),
|
||||||
|
};
|
||||||
|
|
||||||
|
const fakeNotEmptyDB = {
|
||||||
|
query: jest.fn(() => fakeNotEmptyDBQuery),
|
||||||
|
};
|
||||||
|
|
||||||
|
const notEmptyTestStore = createCoreStore({
|
||||||
|
db: fakeNotEmptyDB,
|
||||||
|
});
|
||||||
|
|
||||||
|
const store = notEmptyTestStore({
|
||||||
|
environment: 'test',
|
||||||
|
type: 'plugin',
|
||||||
|
name: 'testName',
|
||||||
|
});
|
||||||
|
|
||||||
|
const rest = await store.delete(data);
|
||||||
|
|
||||||
|
expect(fakeNotEmptyDB.query).toHaveBeenCalledTimes(1);
|
||||||
|
expect(fakeNotEmptyDBQuery.delete).toHaveBeenCalledWith({ where });
|
||||||
|
expect(rest).toEqual(undefined);
|
||||||
|
});
|
||||||
|
});
|
@ -1,102 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
||||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
||||||
};
|
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
|
||||||
const event_hub_1 = __importDefault(require("../event-hub"));
|
|
||||||
describe('Event Hub', () => {
|
|
||||||
it('only triggers the callback once with once()', async () => {
|
|
||||||
const { once, emit } = (0, event_hub_1.default)();
|
|
||||||
const fn = jest.fn();
|
|
||||||
const args = [1, 2, 3];
|
|
||||||
once('my-event', fn);
|
|
||||||
await emit('my-event', ...args);
|
|
||||||
await emit('my-event');
|
|
||||||
await emit('my-event');
|
|
||||||
expect(fn).toHaveBeenCalledTimes(1);
|
|
||||||
expect(fn).toHaveBeenCalledWith(...args);
|
|
||||||
});
|
|
||||||
it('subscribes and unsubscribes to all events', async () => {
|
|
||||||
const { subscribe, unsubscribe, emit } = (0, event_hub_1.default)();
|
|
||||||
const fn = jest.fn();
|
|
||||||
subscribe(fn);
|
|
||||||
const args1 = [1, 2, 3];
|
|
||||||
const args2 = [4, 5, 6];
|
|
||||||
await emit('my-event', ...args1);
|
|
||||||
await emit('my-event', ...args2);
|
|
||||||
await emit('my-other-event');
|
|
||||||
expect(fn).toHaveBeenCalled();
|
|
||||||
expect(fn).toHaveBeenNthCalledWith(1, 'my-event', ...args1);
|
|
||||||
expect(fn).toHaveBeenNthCalledWith(2, 'my-event', ...args2);
|
|
||||||
expect(fn).toHaveBeenNthCalledWith(3, 'my-other-event');
|
|
||||||
// Unsubscribes with unsubscribe()
|
|
||||||
unsubscribe(fn);
|
|
||||||
await emit('my-event');
|
|
||||||
expect(fn).toHaveBeenCalledTimes(3);
|
|
||||||
// Unsubscribes with the returned function
|
|
||||||
const unsubscribe2 = subscribe(fn);
|
|
||||||
await emit('my-event');
|
|
||||||
expect(fn).toHaveBeenCalledTimes(4);
|
|
||||||
unsubscribe2();
|
|
||||||
await emit('my-event');
|
|
||||||
expect(fn).toHaveBeenCalledTimes(4);
|
|
||||||
// Avoid removing the wrong subscriber when unsubscribe is given a non-existing subscriber
|
|
||||||
const unsubscribe3 = subscribe(fn);
|
|
||||||
const unrelatedFunction = jest.fn();
|
|
||||||
unsubscribe(unrelatedFunction);
|
|
||||||
await emit('my-event');
|
|
||||||
expect(fn).toHaveBeenCalledTimes(5);
|
|
||||||
unsubscribe3();
|
|
||||||
});
|
|
||||||
it('adds and removes simple listeners', async () => {
|
|
||||||
const { on, off, emit } = (0, event_hub_1.default)();
|
|
||||||
const fn = jest.fn();
|
|
||||||
const args = [1, 2, 3];
|
|
||||||
// Listens to event with on()
|
|
||||||
on('my-event', fn);
|
|
||||||
await emit('my-event', ...args);
|
|
||||||
expect(fn).toHaveBeenCalledWith(...args);
|
|
||||||
// Removes listener with off()
|
|
||||||
off('my-event', fn);
|
|
||||||
await emit('my-event');
|
|
||||||
expect(fn).toHaveBeenCalledTimes(1);
|
|
||||||
// Removes listener with the returned function
|
|
||||||
const off2 = on('my-event', fn);
|
|
||||||
await emit('my-event', ...args);
|
|
||||||
expect(fn).toHaveBeenCalledTimes(2);
|
|
||||||
off2();
|
|
||||||
await emit('my-event');
|
|
||||||
expect(fn).toHaveBeenCalledTimes(2);
|
|
||||||
});
|
|
||||||
it('removes all subscribers on destroy()', async () => {
|
|
||||||
const { subscribe, on, emit, destroy } = (0, event_hub_1.default)();
|
|
||||||
const fn = jest.fn();
|
|
||||||
const fn2 = jest.fn();
|
|
||||||
subscribe(fn);
|
|
||||||
on('my-event', fn2);
|
|
||||||
await emit('my-event');
|
|
||||||
expect(fn).toHaveBeenCalled();
|
|
||||||
expect(fn2).toHaveBeenCalled();
|
|
||||||
destroy();
|
|
||||||
// Subscribers are removed
|
|
||||||
await emit('my-event');
|
|
||||||
expect(fn).toHaveBeenCalledTimes(1);
|
|
||||||
expect(fn2).toHaveBeenCalledTimes(1);
|
|
||||||
});
|
|
||||||
it('removes all subscribers on removeAllListeners()', async () => {
|
|
||||||
const { subscribe, on, emit, removeAllListeners } = (0, event_hub_1.default)();
|
|
||||||
const fn = jest.fn();
|
|
||||||
const fn2 = jest.fn();
|
|
||||||
subscribe(fn);
|
|
||||||
on('my-event', fn2);
|
|
||||||
await emit('my-event');
|
|
||||||
expect(fn).toHaveBeenCalled();
|
|
||||||
expect(fn2).toHaveBeenCalled();
|
|
||||||
removeAllListeners();
|
|
||||||
// Subscribers are removed
|
|
||||||
await emit('my-event');
|
|
||||||
expect(fn).toHaveBeenCalledTimes(1);
|
|
||||||
expect(fn2).toHaveBeenCalledTimes(1);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
//# sourceMappingURL=event-hub.test.js.map
|
|
124
packages/core/strapi/src/services/__tests__/event-hub.test.ts
Normal file
124
packages/core/strapi/src/services/__tests__/event-hub.test.ts
Normal file
@ -0,0 +1,124 @@
|
|||||||
|
import createEventHub from '../event-hub';
|
||||||
|
|
||||||
|
describe('Event Hub', () => {
|
||||||
|
it('only triggers the callback once with once()', async () => {
|
||||||
|
const { once, emit } = createEventHub();
|
||||||
|
|
||||||
|
const fn = jest.fn();
|
||||||
|
|
||||||
|
const args = [1, 2, 3];
|
||||||
|
once('my-event', fn);
|
||||||
|
|
||||||
|
await emit('my-event', ...args);
|
||||||
|
await emit('my-event');
|
||||||
|
await emit('my-event');
|
||||||
|
|
||||||
|
expect(fn).toHaveBeenCalledTimes(1);
|
||||||
|
expect(fn).toHaveBeenCalledWith(...args);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('subscribes and unsubscribes to all events', async () => {
|
||||||
|
const { subscribe, unsubscribe, emit } = createEventHub();
|
||||||
|
|
||||||
|
const fn = jest.fn();
|
||||||
|
subscribe(fn);
|
||||||
|
|
||||||
|
const args1 = [1, 2, 3];
|
||||||
|
const args2 = [4, 5, 6];
|
||||||
|
|
||||||
|
await emit('my-event', ...args1);
|
||||||
|
await emit('my-event', ...args2);
|
||||||
|
await emit('my-other-event');
|
||||||
|
|
||||||
|
expect(fn).toHaveBeenCalled();
|
||||||
|
expect(fn).toHaveBeenNthCalledWith(1, 'my-event', ...args1);
|
||||||
|
expect(fn).toHaveBeenNthCalledWith(2, 'my-event', ...args2);
|
||||||
|
expect(fn).toHaveBeenNthCalledWith(3, 'my-other-event');
|
||||||
|
|
||||||
|
// Unsubscribes with unsubscribe()
|
||||||
|
unsubscribe(fn);
|
||||||
|
await emit('my-event');
|
||||||
|
expect(fn).toHaveBeenCalledTimes(3);
|
||||||
|
|
||||||
|
// Unsubscribes with the returned function
|
||||||
|
const unsubscribe2 = subscribe(fn);
|
||||||
|
await emit('my-event');
|
||||||
|
expect(fn).toHaveBeenCalledTimes(4);
|
||||||
|
unsubscribe2();
|
||||||
|
await emit('my-event');
|
||||||
|
expect(fn).toHaveBeenCalledTimes(4);
|
||||||
|
|
||||||
|
// Avoid removing the wrong subscriber when unsubscribe is given a non-existing subscriber
|
||||||
|
const unsubscribe3 = subscribe(fn);
|
||||||
|
const unrelatedFunction = jest.fn();
|
||||||
|
unsubscribe(unrelatedFunction);
|
||||||
|
await emit('my-event');
|
||||||
|
expect(fn).toHaveBeenCalledTimes(5);
|
||||||
|
unsubscribe3();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('adds and removes simple listeners', async () => {
|
||||||
|
const { on, off, emit } = createEventHub();
|
||||||
|
|
||||||
|
const fn = jest.fn();
|
||||||
|
const args = [1, 2, 3];
|
||||||
|
|
||||||
|
// Listens to event with on()
|
||||||
|
on('my-event', fn);
|
||||||
|
await emit('my-event', ...args);
|
||||||
|
expect(fn).toHaveBeenCalledWith(...args);
|
||||||
|
|
||||||
|
// Removes listener with off()
|
||||||
|
off('my-event', fn);
|
||||||
|
await emit('my-event');
|
||||||
|
expect(fn).toHaveBeenCalledTimes(1);
|
||||||
|
|
||||||
|
// Removes listener with the returned function
|
||||||
|
const off2 = on('my-event', fn);
|
||||||
|
await emit('my-event', ...args);
|
||||||
|
expect(fn).toHaveBeenCalledTimes(2);
|
||||||
|
off2();
|
||||||
|
await emit('my-event');
|
||||||
|
expect(fn).toHaveBeenCalledTimes(2);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('removes all subscribers on destroy()', async () => {
|
||||||
|
const { subscribe, on, emit, destroy } = createEventHub();
|
||||||
|
|
||||||
|
const fn = jest.fn();
|
||||||
|
const fn2 = jest.fn();
|
||||||
|
subscribe(fn);
|
||||||
|
on('my-event', fn2);
|
||||||
|
|
||||||
|
await emit('my-event');
|
||||||
|
expect(fn).toHaveBeenCalled();
|
||||||
|
expect(fn2).toHaveBeenCalled();
|
||||||
|
|
||||||
|
destroy();
|
||||||
|
|
||||||
|
// Subscribers are removed
|
||||||
|
await emit('my-event');
|
||||||
|
expect(fn).toHaveBeenCalledTimes(1);
|
||||||
|
expect(fn2).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('removes all subscribers on removeAllListeners()', async () => {
|
||||||
|
const { subscribe, on, emit, removeAllListeners } = createEventHub();
|
||||||
|
|
||||||
|
const fn = jest.fn();
|
||||||
|
const fn2 = jest.fn();
|
||||||
|
subscribe(fn);
|
||||||
|
on('my-event', fn2);
|
||||||
|
|
||||||
|
await emit('my-event');
|
||||||
|
expect(fn).toHaveBeenCalled();
|
||||||
|
expect(fn2).toHaveBeenCalled();
|
||||||
|
|
||||||
|
removeAllListeners();
|
||||||
|
|
||||||
|
// Subscribers are removed
|
||||||
|
await emit('my-event');
|
||||||
|
expect(fn).toHaveBeenCalledTimes(1);
|
||||||
|
expect(fn2).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
});
|
@ -1,55 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
||||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
||||||
};
|
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
|
||||||
const path_1 = __importDefault(require("path"));
|
|
||||||
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
||||||
const fs_1 = __importDefault(require("../fs"));
|
|
||||||
jest.mock('fs-extra', () => ({
|
|
||||||
ensureFile: jest.fn(() => Promise.resolve()),
|
|
||||||
writeFile: jest.fn(() => Promise.resolve()),
|
|
||||||
}));
|
|
||||||
describe('Strapi fs utils', () => {
|
|
||||||
const strapi = {
|
|
||||||
dirs: { dist: { root: '/tmp' }, app: { root: '/tmp' } },
|
|
||||||
};
|
|
||||||
test('Provides new functions', () => {
|
|
||||||
const strapiFS = (0, fs_1.default)(strapi);
|
|
||||||
expect(strapiFS.writeAppFile).toBeInstanceOf(Function);
|
|
||||||
expect(strapiFS.writePluginFile).toBeInstanceOf(Function);
|
|
||||||
});
|
|
||||||
describe('Write App File', () => {
|
|
||||||
test('Makes sure the path exists and writes', async () => {
|
|
||||||
const strapiFS = (0, fs_1.default)(strapi);
|
|
||||||
const content = '';
|
|
||||||
await strapiFS.writeAppFile('test', content);
|
|
||||||
expect(fs_extra_1.default.ensureFile).toHaveBeenCalledWith(path_1.default.join('/', 'tmp', 'test'));
|
|
||||||
expect(fs_extra_1.default.writeFile).toHaveBeenCalledWith(path_1.default.join('/', 'tmp', 'test'), content);
|
|
||||||
});
|
|
||||||
test('Normalize the path to avoid relative access to folders in parent directories', async () => {
|
|
||||||
const strapiFS = (0, fs_1.default)(strapi);
|
|
||||||
const content = '';
|
|
||||||
await strapiFS.writeAppFile('../../test', content);
|
|
||||||
expect(fs_extra_1.default.ensureFile).toHaveBeenCalledWith(path_1.default.join('/', 'tmp', 'test'));
|
|
||||||
expect(fs_extra_1.default.writeFile).toHaveBeenCalledWith(path_1.default.join('/', 'tmp', 'test'), content);
|
|
||||||
});
|
|
||||||
test('Works with array path', async () => {
|
|
||||||
const strapiFS = (0, fs_1.default)(strapi);
|
|
||||||
const content = '';
|
|
||||||
await strapiFS.writeAppFile(['test', 'sub', 'path'], content);
|
|
||||||
expect(fs_extra_1.default.ensureFile).toHaveBeenCalledWith(path_1.default.join('/', 'tmp', 'test', 'sub', 'path'));
|
|
||||||
expect(fs_extra_1.default.writeFile).toHaveBeenCalledWith(path_1.default.join('/', 'tmp', 'test', 'sub', 'path'), content);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
describe('Write Plugin File', () => {
|
|
||||||
test('Scopes the writes in the extensions folder', async () => {
|
|
||||||
const strapiFS = (0, fs_1.default)(strapi);
|
|
||||||
const content = '';
|
|
||||||
strapiFS.writeAppFile = jest.fn(() => Promise.resolve());
|
|
||||||
await strapiFS.writePluginFile('users-permissions', ['test', 'sub', 'path'], content);
|
|
||||||
expect(strapiFS.writeAppFile).toHaveBeenCalledWith('extensions/users-permissions/test/sub/path', content);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
//# sourceMappingURL=fs.test.js.map
|
|
76
packages/core/strapi/src/services/__tests__/fs.test.ts
Normal file
76
packages/core/strapi/src/services/__tests__/fs.test.ts
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
import path from 'path';
|
||||||
|
import fse from 'fs-extra';
|
||||||
|
import fs from '../fs';
|
||||||
|
|
||||||
|
jest.mock('fs-extra', () => ({
|
||||||
|
ensureFile: jest.fn(() => Promise.resolve()),
|
||||||
|
writeFile: jest.fn(() => Promise.resolve()),
|
||||||
|
}));
|
||||||
|
|
||||||
|
describe('Strapi fs utils', () => {
|
||||||
|
const strapi = {
|
||||||
|
dirs: { dist: { root: '/tmp' }, app: { root: '/tmp' } },
|
||||||
|
};
|
||||||
|
|
||||||
|
test('Provides new functions', () => {
|
||||||
|
const strapiFS = fs(strapi);
|
||||||
|
|
||||||
|
expect(strapiFS.writeAppFile).toBeInstanceOf(Function);
|
||||||
|
expect(strapiFS.writePluginFile).toBeInstanceOf(Function);
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Write App File', () => {
|
||||||
|
test('Makes sure the path exists and writes', async () => {
|
||||||
|
const strapiFS = fs(strapi);
|
||||||
|
|
||||||
|
const content = '';
|
||||||
|
|
||||||
|
await strapiFS.writeAppFile('test', content);
|
||||||
|
|
||||||
|
expect(fse.ensureFile).toHaveBeenCalledWith(path.join('/', 'tmp', 'test'));
|
||||||
|
expect(fse.writeFile).toHaveBeenCalledWith(path.join('/', 'tmp', 'test'), content);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Normalize the path to avoid relative access to folders in parent directories', async () => {
|
||||||
|
const strapiFS = fs(strapi);
|
||||||
|
|
||||||
|
const content = '';
|
||||||
|
|
||||||
|
await strapiFS.writeAppFile('../../test', content);
|
||||||
|
|
||||||
|
expect(fse.ensureFile).toHaveBeenCalledWith(path.join('/', 'tmp', 'test'));
|
||||||
|
expect(fse.writeFile).toHaveBeenCalledWith(path.join('/', 'tmp', 'test'), content);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Works with array path', async () => {
|
||||||
|
const strapiFS = fs(strapi);
|
||||||
|
|
||||||
|
const content = '';
|
||||||
|
|
||||||
|
await strapiFS.writeAppFile(['test', 'sub', 'path'], content);
|
||||||
|
|
||||||
|
expect(fse.ensureFile).toHaveBeenCalledWith(path.join('/', 'tmp', 'test', 'sub', 'path'));
|
||||||
|
expect(fse.writeFile).toHaveBeenCalledWith(
|
||||||
|
path.join('/', 'tmp', 'test', 'sub', 'path'),
|
||||||
|
content
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Write Plugin File', () => {
|
||||||
|
test('Scopes the writes in the extensions folder', async () => {
|
||||||
|
const strapiFS = fs(strapi);
|
||||||
|
|
||||||
|
const content = '';
|
||||||
|
|
||||||
|
strapiFS.writeAppFile = jest.fn(() => Promise.resolve());
|
||||||
|
|
||||||
|
await strapiFS.writePluginFile('users-permissions', ['test', 'sub', 'path'], content);
|
||||||
|
|
||||||
|
expect(strapiFS.writeAppFile).toHaveBeenCalledWith(
|
||||||
|
'extensions/users-permissions/test/sub/path',
|
||||||
|
content
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
@ -1,41 +0,0 @@
|
|||||||
"use strict";
|
|
||||||
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
||||||
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
||||||
};
|
|
||||||
Object.defineProperty(exports, "__esModule", { value: true });
|
|
||||||
const worker_queue_1 = __importDefault(require("../worker-queue"));
|
|
||||||
describe('WorkerQueue', () => {
|
|
||||||
test('Executes worker', async () => {
|
|
||||||
const fn = jest.fn();
|
|
||||||
const input = 1;
|
|
||||||
const q = new worker_queue_1.default({
|
|
||||||
logger: console.log.bind(console),
|
|
||||||
concurrency: 1,
|
|
||||||
});
|
|
||||||
q.subscribe(fn);
|
|
||||||
q.enqueue(input);
|
|
||||||
await new Promise((resolve) => {
|
|
||||||
setTimeout(resolve);
|
|
||||||
});
|
|
||||||
expect(fn).toHaveBeenCalledWith(input);
|
|
||||||
expect(fn).toHaveBeenCalledTimes(1);
|
|
||||||
});
|
|
||||||
test('Executes worker', async () => {
|
|
||||||
const fn = jest.fn();
|
|
||||||
const input = 1;
|
|
||||||
const q = new worker_queue_1.default({
|
|
||||||
logger: console.log.bind(console),
|
|
||||||
concurrency: 1,
|
|
||||||
});
|
|
||||||
q.subscribe(fn);
|
|
||||||
q.enqueue(input);
|
|
||||||
q.enqueue(input);
|
|
||||||
q.enqueue(input);
|
|
||||||
await new Promise((resolve) => {
|
|
||||||
setTimeout(resolve);
|
|
||||||
});
|
|
||||||
expect(fn).toHaveBeenCalledWith(input);
|
|
||||||
expect(fn).toHaveBeenCalledTimes(3);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
//# sourceMappingURL=worker-queue.test.js.map
|
|
@ -0,0 +1,45 @@
|
|||||||
|
import WorkerQueue from '../worker-queue';
|
||||||
|
|
||||||
|
describe('WorkerQueue', () => {
|
||||||
|
test('Executes worker', async () => {
|
||||||
|
const fn = jest.fn();
|
||||||
|
const input = 1;
|
||||||
|
|
||||||
|
const q = new WorkerQueue({
|
||||||
|
logger: console.log.bind(console),
|
||||||
|
concurrency: 1,
|
||||||
|
});
|
||||||
|
q.subscribe(fn);
|
||||||
|
|
||||||
|
q.enqueue(input);
|
||||||
|
|
||||||
|
await new Promise((resolve) => {
|
||||||
|
setTimeout(resolve);
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(fn).toHaveBeenCalledWith(input);
|
||||||
|
expect(fn).toHaveBeenCalledTimes(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Executes worker', async () => {
|
||||||
|
const fn = jest.fn();
|
||||||
|
const input = 1;
|
||||||
|
|
||||||
|
const q = new WorkerQueue({
|
||||||
|
logger: console.log.bind(console),
|
||||||
|
concurrency: 1,
|
||||||
|
});
|
||||||
|
q.subscribe(fn);
|
||||||
|
|
||||||
|
q.enqueue(input);
|
||||||
|
q.enqueue(input);
|
||||||
|
q.enqueue(input);
|
||||||
|
|
||||||
|
await new Promise((resolve) => {
|
||||||
|
setTimeout(resolve);
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(fn).toHaveBeenCalledWith(input);
|
||||||
|
expect(fn).toHaveBeenCalledTimes(3);
|
||||||
|
});
|
||||||
|
});
|
Loading…
x
Reference in New Issue
Block a user