Skip to content

Commit

Permalink
test(): Add test for empty relationIds on add/remove
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-martin committed Apr 13, 2021
1 parent 84b5b5c commit 227d02d
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1218,6 +1218,21 @@ describe('MongooseQueryService', () => {
expect(relations).toHaveLength(6);
});

it('should not modify relations if relationIds is empty', async () => {
const entity = TEST_ENTITIES[0];
const queryService = moduleRef.get(TestEntityService);
const queryResult = await queryService.addRelations('testReferences', entity._id, []);
expect(queryResult).toEqual(
expect.objectContaining({
_id: entity._id,
testReferences: expect.arrayContaining(TEST_REFERENCES.slice(0, 3).map((r) => r._id)),
}),
);

const relations = await queryService.queryRelations(TestReference, 'testReferences', entity, {});
expect(relations).toHaveLength(3);
});

describe('with virtual reference', () => {
it('should return a rejected promise', async () => {
const entity = TEST_ENTITIES[0];
Expand Down Expand Up @@ -1392,6 +1407,21 @@ describe('MongooseQueryService', () => {
expect(relations).toHaveLength(0);
});

it('should not modify relations if relationIds is empty', async () => {
const entity = TEST_ENTITIES[0];
const queryService = moduleRef.get(TestEntityService);
const queryResult = await queryService.removeRelations('testReferences', entity._id, []);
expect(queryResult.toObject()).toEqual(
expect.objectContaining({
_id: entity._id,
testReferences: expect.arrayContaining(TEST_REFERENCES.slice(0, 3).map((r) => r._id)),
}),
);

const relations = await queryService.queryRelations(TestReference, 'testReferences', entity, {});
expect(relations).toHaveLength(3);
});

describe('with virtual reference', () => {
it('should return a rejected promise', async () => {
const entity = TEST_ENTITIES[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,16 @@ describe('SequelizeQueryService', (): void => {
expect(relations).toHaveLength(6);
});

it('should not modify relations if the relationIds is empty', async () => {
const entity = PLAIN_TEST_ENTITIES[0] as TestEntity;
const queryService = moduleRef.get(TestEntityService);
const queryResult = await queryService.addRelations('testRelations', entity.testEntityPk, []);
expect(queryResult).toEqual(expect.objectContaining(entity));

const relations = await queryService.queryRelations(TestRelation, 'testRelations', entity, {});
expect(relations).toHaveLength(3);
});

describe('with modify options', () => {
it('should throw an error if the entity is not found with the id and provided filter', async () => {
const entity = PLAIN_TEST_ENTITIES[0];
Expand Down Expand Up @@ -980,6 +990,16 @@ describe('SequelizeQueryService', (): void => {
expect(relations).toHaveLength(0);
});

it('should not modify relations if relationIds is empty', async () => {
const entity = PLAIN_TEST_ENTITIES[0] as TestEntity;
const queryService = moduleRef.get(TestEntityService);
const queryResult = await queryService.removeRelations('testRelations', entity.testEntityPk, []);
expect(queryResult).toEqual(expect.objectContaining(entity));

const relations = await queryService.queryRelations(TestRelation, 'testRelations', entity, {});
expect(relations).toHaveLength(3);
});

describe('with modify options', () => {
it('should throw an error if the entity is not found with the id and provided filter', async () => {
const entity = PLAIN_TEST_ENTITIES[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1222,6 +1222,21 @@ describe('TypegooseQueryService', () => {
expect(relations).toHaveLength(6);
});

it('should not modify relations if relationIds is empty', async () => {
const entity = TEST_ENTITIES[0];
const queryService = moduleRef.get(TestEntityService);
const queryResult = await queryService.addRelations('testReferences', entity._id.toHexString(), []);
expect(queryResult).toEqual(
expect.objectContaining({
_id: entity._id,
testReferences: expect.arrayContaining(TEST_REFERENCES.slice(0, 3).map((r) => r._id)),
}),
);

const relations = await queryService.queryRelations(TestReference, 'testReferences', entity, {});
expect(relations).toHaveLength(3);
});

describe('with virtual reference', () => {
it('should return a rejected promise', async () => {
const entity = TEST_ENTITIES[0];
Expand Down Expand Up @@ -1461,6 +1476,21 @@ describe('TypegooseQueryService', () => {
expect(relations).toHaveLength(0);
});

it('should not modify relations if relationIds is empty', async () => {
const entity = TEST_ENTITIES[0];
const queryService = moduleRef.get(TestEntityService);
const queryResult = await queryService.removeRelations('testReferences', entity._id.toHexString(), []);
expect(queryResult.toObject()).toEqual(
expect.objectContaining({
_id: entity._id,
testReferences: expect.arrayContaining(TEST_REFERENCES.slice(0, 3).map((r) => r._id)),
}),
);

const relations = await queryService.queryRelations(TestReference, 'testReferences', entity, {});
expect(relations).toHaveLength(3);
});

describe('with virtual reference', () => {
it('should return a rejected promise', async () => {
const entity = TEST_ENTITIES[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,16 @@ describe('TypeOrmQueryService', (): void => {
expect(relations).toHaveLength(6);
});

it('should not modify if the relationIds is empty', async () => {
const entity = TEST_ENTITIES[0];
const queryService = moduleRef.get(TestEntityService);
const queryResult = await queryService.addRelations('testRelations', entity.testEntityPk, []);
expect(queryResult).toEqual(entity);

const relations = await queryService.queryRelations(TestRelation, 'testRelations', entity, {});
expect(relations).toHaveLength(3);
});

describe('with modify options', () => {
it('should throw an error if the entity is not found with the id and provided filter', async () => {
const entity = TEST_ENTITIES[0];
Expand Down Expand Up @@ -1284,6 +1294,16 @@ describe('TypeOrmQueryService', (): void => {
expect(relations).toHaveLength(0);
});

it('should not remove any relations if relationIds is empty', async () => {
const entity = TEST_ENTITIES[0];
const queryService = moduleRef.get(TestEntityService);
const queryResult = await queryService.removeRelations('testRelations', entity.testEntityPk, []);
expect(queryResult).toEqual(entity);

const relations = await queryService.queryRelations(TestRelation, 'testRelations', entity, {});
expect(relations).toHaveLength(3);
});

describe('with modify options', () => {
it('should throw an error if the entity is not found with the id and provided filter', async () => {
const entity = TEST_ENTITIES[0];
Expand Down

0 comments on commit 227d02d

Please sign in to comment.