2020-07-02 14:00:07 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const createContext = require('../../../../test/helpers/create-context');
|
2020-10-27 16:01:46 +01:00
|
|
|
const relations = require('../relations');
|
2020-07-02 14:00:07 +02:00
|
|
|
|
2020-10-27 16:01:46 +01:00
|
|
|
describe('Relations', () => {
|
|
|
|
describe('find', () => {
|
2020-07-02 14:00:07 +02:00
|
|
|
test('Fails on model not found', async () => {
|
|
|
|
const notFound = jest.fn();
|
|
|
|
const ctx = createContext(
|
|
|
|
{
|
|
|
|
params: { model: 'test', targetField: 'field' },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
notFound,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const getModel = jest.fn();
|
2020-07-02 17:52:27 +02:00
|
|
|
global.strapi = {
|
|
|
|
db: { getModel },
|
|
|
|
plugins: {
|
|
|
|
'content-manager': {
|
|
|
|
services: {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
2020-07-02 14:00:07 +02:00
|
|
|
|
2020-10-27 16:01:46 +01:00
|
|
|
await relations.find(ctx);
|
2020-07-02 14:00:07 +02:00
|
|
|
|
|
|
|
expect(notFound).toHaveBeenCalledWith('model.notFound');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Fails on invalid target field', async () => {
|
|
|
|
const badRequest = jest.fn();
|
|
|
|
const ctx = createContext(
|
|
|
|
{
|
|
|
|
params: { model: 'test', targetField: 'field' },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
badRequest,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const getModel = jest.fn(() => ({
|
|
|
|
attributes: {},
|
|
|
|
}));
|
|
|
|
|
2020-07-02 17:52:27 +02:00
|
|
|
global.strapi = {
|
|
|
|
db: { getModel },
|
|
|
|
plugins: {
|
|
|
|
'content-manager': {
|
|
|
|
services: {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
2020-07-02 14:00:07 +02:00
|
|
|
|
2020-10-27 16:01:46 +01:00
|
|
|
await relations.find(ctx);
|
2020-07-02 14:00:07 +02:00
|
|
|
|
|
|
|
expect(badRequest).toHaveBeenCalledWith('targetField.invalid');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Fails on model not found', async () => {
|
|
|
|
const notFound = jest.fn();
|
|
|
|
const ctx = createContext(
|
|
|
|
{
|
|
|
|
params: { model: 'test', targetField: 'target' },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
notFound,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const getModelByAssoc = jest.fn();
|
|
|
|
const getModel = jest.fn(() => ({
|
|
|
|
attributes: { target: { model: 'test' } },
|
|
|
|
}));
|
2020-07-02 17:52:27 +02:00
|
|
|
global.strapi = {
|
|
|
|
db: { getModel, getModelByAssoc },
|
|
|
|
plugins: {
|
|
|
|
'content-manager': {
|
|
|
|
services: {},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
2020-07-02 14:00:07 +02:00
|
|
|
|
2020-10-27 16:01:46 +01:00
|
|
|
await relations.find(ctx);
|
2020-07-02 14:00:07 +02:00
|
|
|
|
|
|
|
expect(notFound).toHaveBeenCalledWith('target.notFound');
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Picks the mainField and primaryKey / id only', async () => {
|
|
|
|
const notFound = jest.fn();
|
|
|
|
const ctx = createContext(
|
|
|
|
{
|
|
|
|
params: { model: 'test', targetField: 'target' },
|
|
|
|
},
|
|
|
|
{
|
|
|
|
notFound,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
const getModelByAssoc = jest.fn(() => ({ primaryKey: 'id', attributes: {} }));
|
|
|
|
const getModel = jest.fn(() => ({ attributes: { target: { model: 'test' } } }));
|
|
|
|
|
|
|
|
global.strapi = {
|
|
|
|
db: {
|
|
|
|
getModel,
|
|
|
|
getModelByAssoc,
|
|
|
|
},
|
|
|
|
plugins: {
|
|
|
|
'content-manager': {
|
|
|
|
services: {
|
2020-10-27 16:01:46 +01:00
|
|
|
'content-types': {
|
|
|
|
findConfiguration() {
|
2020-07-02 14:00:07 +02:00
|
|
|
return {
|
|
|
|
metadatas: {
|
|
|
|
target: {
|
|
|
|
edit: {
|
|
|
|
mainField: 'title',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
},
|
|
|
|
},
|
2020-11-02 12:40:35 +01:00
|
|
|
entity: {
|
|
|
|
find() {
|
2020-07-02 14:00:07 +02:00
|
|
|
return [
|
|
|
|
{
|
|
|
|
id: 1,
|
|
|
|
title: 'title1',
|
|
|
|
secret: 'some secret',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 2,
|
|
|
|
title: 'title2',
|
|
|
|
secret: 'some secret 2',
|
|
|
|
},
|
|
|
|
];
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2020-10-27 16:01:46 +01:00
|
|
|
await relations.find(ctx);
|
2020-07-02 14:00:07 +02:00
|
|
|
|
|
|
|
expect(ctx.body).toEqual([
|
|
|
|
{
|
|
|
|
id: 1,
|
|
|
|
title: 'title1',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 2,
|
|
|
|
title: 'title2',
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|