Skip to content

Commit

Permalink
test: unit test for entityToData
Browse files Browse the repository at this point in the history
  • Loading branch information
Agnes Lin committed Dec 3, 2019
1 parent a476fbe commit f55e2ac
Showing 1 changed file with 13 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,19 @@ describe('DefaultCrudRepository', () => {
});
});

it('throws if the target data passes to CRUD methods contains nav properties', async () => {
// a unit test for entityToData, which is invoked by create() method
// it would be the same of other CRUD methods.
await expect(
folderRepo.create({
name: 'f1',
files: [{title: 'nav property'}],
}),
).to.be.rejectedWith(
'Navigational properties are not allowed in model data (model "Folder" property "files")',
);
});

// stub resolvers
const hasManyResolver: InclusionResolver<
Folder,
Expand Down Expand Up @@ -503,122 +516,6 @@ describe('DefaultCrudRepository', () => {
return authors;
};
});
context(
'checks navigational properties for create and update operations (ensurePersistable)',
() => {
it('throws if create() tries to create an entity with DataObject with nav properties', async () => {
await expect(
folderRepo.create({
name: 'f1',
files: [{title: 'nav property'}],
}),
).to.be.rejectedWith(
'Navigational properties are not allowed in model data (model "Folder" property "files")',
);
});

it('throws if createAll() tries to create all entities that some if them have navigational properties', async () => {
await expect(
folderRepo.createAll([
{name: 'f1'},
{name: 'f2', files: [{title: 'nav property'}]},
]),
).to.be.rejectedWith(
'Navigational properties are not allowed in model data (model "Folder" property "files")',
);
});
it('throws if updateAll() tries to update entities with DataObject with nav properties', async () => {
await folderRepo.create({name: 'f1', id: 1});
await folderRepo.create({name: 'f2', id: 2});
await expect(
folderRepo.updateAll({
name: 'unified name',
files: [{title: 'nav property'}],
}),
).to.be.rejectedWith(
'Navigational properties are not allowed in model data (model "Folder" property "files")',
);
});

it('throws if updateById() tries to update entities with an entity with nav properties', async () => {
folderRepo.registerInclusionResolver('files', hasManyResolver);
await folderRepo.create({name: 'folder', id: 1});

await expect(
folderRepo.updateById(1, {
name: 'update',
files: [{title: 't', id: 1, folderId: 1}],
}),
).to.be.rejectedWith(
'Navigational properties are not allowed in model data (model "Folder" property "files")',
);
});

it('throws if update() tries to update entities with Entity.toJSON() with nav properties', async () => {
folderRepo.registerInclusionResolver('files', hasManyResolver);
await folderRepo.create({name: 'folder', id: 1});
await fileRepo.create({title: 'file title', id: 1, folderId: 1});
const folder = await folderRepo.findById(1, {
include: [{relation: 'files'}],
});
folder.name = 'new name';

await expect(folderRepo.update(folder)).to.be.rejectedWith(
'Navigational properties are not allowed in model data (model "Folder" property "files")',
);
});
// only test replaceById and save with normal id type here. Tests for MongoDB are in repository-tests
it('implements Repository.save() without id', async () => {
const fol = await folderRepo.create({name: 'folder'});
const folder = await folderRepo.save(fol);
const result = await folderRepo.findById(folder!.id);
expect(result.toJSON()).to.eql(fol!.toJSON());
});

it('implements Repository.save() with id', async () => {
const fol = await folderRepo.create({name: 'f3', id: 1});
fol.name = 'new name';
const saved = await folderRepo.save(fol);
const result = await folderRepo.findById(saved!.id);
expect(result.toJSON()).to.eql(fol.toJSON());
});

it('implements Repository.replaceById() without id property provided in the target data', async () => {
const fol = await folderRepo.create({name: 'f3', id: 1});
await folderRepo.replaceById(fol.id, {
name: 'new name',
});
const result = await folderRepo.findById(fol.id);
expect(result.toJSON()).to.eql({
id: fol.id,
name: 'new name',
});
});

it('throws when Repository.replaceById() with id property provided in the target data', async () => {
const fol = await folderRepo.create({name: 'f3', id: 1});
fol.name = 'new name';
await folderRepo.replaceById(fol.id, fol);
const result = await folderRepo.findById(fol.id);
expect(result.toJSON()).to.eql({
id: fol.id,
name: 'new name',
});
});

const hasManyResolver: InclusionResolver<
Folder,
File
> = async entities => {
const files = [];
for (const entity of entities) {
const file = await folderFiles(entity.id).find();
files.push(file);
}
return files;
};
},
);
});

it('implements Repository.delete()', async () => {
Expand Down

0 comments on commit f55e2ac

Please sign in to comment.