added tests for restore

This commit is contained in:
Bassel 2022-11-24 14:23:50 +02:00
parent 2ce7a23327
commit afbfffe424
2 changed files with 68 additions and 55 deletions

View File

@ -107,61 +107,6 @@ describe('Local Strapi Source Destination', () => {
expect(deleteAllSpy).toBeCalledTimes(1);
});
test('Should delete only chosen contentTypes', async () => {
const entities = [
{
entity: { id: 1, title: 'My first foo' },
contentType: { uid: 'foo' },
},
{
entity: { id: 4, title: 'Another Foo' },
contentType: { uid: 'foo' },
},
{
entity: { id: 12, title: 'Last foo' },
contentType: { uid: 'foo' },
},
{
entity: { id: 1, age: 21 },
contentType: { uid: 'bar' },
},
{
entity: { id: 2, age: 42 },
contentType: { uid: 'bar' },
},
{
entity: { id: 7, age: 84 },
contentType: { uid: 'bar' },
},
{
entity: { id: 9, age: 0 },
contentType: { uid: 'bar' },
},
];
const deleteMany = jest.fn(async (uid: string) => ({
count: entities.filter((entity) => entity.contentType.uid === uid).length,
}));
const provider = createLocalStrapiDestinationProvider({
getStrapi: getStrapiFactory({
contentTypes: getContentTypes(),
entityService: {
deleteMany,
},
}),
strategy: 'restore',
restore: {
/* @ts-ignore: disable-next-line */
contentTypes: [getContentTypes()['foo']],
},
});
const deleteAllSpy = jest.spyOn(restoreApi, 'deleteAllRecords');
await provider.bootstrap();
await provider.beforeStreaming();
expect(deleteAllSpy).toBeCalledTimes(1);
});
test('Should not delete if it is a merge strategy', async () => {
const provider = createLocalStrapiDestinationProvider({
getStrapi: getStrapiFactory({}),

View File

@ -0,0 +1,68 @@
import { deleteAllRecords } from '../restore';
import { getStrapiFactory, getContentTypes } from '../../test-utils';
const entities = [
{
entity: { id: 1, title: 'My first foo' },
contentType: { uid: 'foo' },
},
{
entity: { id: 4, title: 'Another Foo' },
contentType: { uid: 'foo' },
},
{
entity: { id: 12, title: 'Last foo' },
contentType: { uid: 'foo' },
},
{
entity: { id: 1, age: 21 },
contentType: { uid: 'bar' },
},
{
entity: { id: 2, age: 42 },
contentType: { uid: 'bar' },
},
{
entity: { id: 7, age: 84 },
contentType: { uid: 'bar' },
},
{
entity: { id: 9, age: 0 },
contentType: { uid: 'bar' },
},
];
describe('Restore ', () => {
test('Should delete all contentTypes', async () => {
const deleteMany = jest.fn(async (uid: string) => ({
count: entities.filter((entity) => entity.contentType.uid === uid).length,
}));
const strapi = getStrapiFactory({
contentTypes: getContentTypes(),
entityService: {
deleteMany,
},
})();
const { count } = await deleteAllRecords(strapi);
expect(count).toBe(entities.length);
});
test('Should only delete chosen contentTypes', async () => {
const deleteMany = jest.fn(async (uid: string) => ({
count: entities.filter((entity) => entity.contentType.uid === uid).length,
}));
const strapi = getStrapiFactory({
contentTypes: getContentTypes(),
entityService: {
deleteMany,
},
})();
const { count } = await deleteAllRecords(strapi, {
/* @ts-ignore: disable-next-line */
contentTypes: [getContentTypes()['foo']],
});
expect(count).toBe(3);
});
});