Skip to content

Commit

Permalink
fix(tests): fix jest-extended typings and eslint problems
Browse files Browse the repository at this point in the history
  • Loading branch information
doug-martin committed Sep 30, 2021
1 parent f539b29 commit 6af8af1
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 41 deletions.
1 change: 1 addition & 0 deletions jest.e2e.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ module.exports = {
moduleNameMapper: {
'^@nestjs-query/(.*)$': '<rootDir>/packages/$1/src',
},
"setupFilesAfterEnv": ["jest-extended"],
snapshotSerializers: ['jest-snapshot-serializer-raw/always']
};
1 change: 1 addition & 0 deletions jest.unit.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ module.exports = {
moduleNameMapper: {
'^@nestjs-query/(.*)$': '<rootDir>/packages/$1/src',
},
"setupFilesAfterEnv": ["jest-extended"],
snapshotSerializers: ['jest-snapshot-serializer-raw/always']
};

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('FilterQueryBuilder', (): void => {
oneTestRelation: { relationOfTestRelation: {} },
});
});
it('with nested and / or', () => {
it('with nested and / or', () => {
const mockWhereBuilder = mock<WhereBuilder<TestEntity>>(WhereBuilder);
const qb = getEntityQueryBuilder(TestEntity, instance(mockWhereBuilder));
expect(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'jest-extended';
import { Filter, SortDirection } from '@nestjs-query/core';
import { Test, TestingModule } from '@nestjs/testing';
import { plainToClass } from 'class-transformer';
Expand Down Expand Up @@ -156,10 +155,10 @@ describe('TypeOrmQueryService', (): void => {
},
},
});
expect(queryResults).toBeArrayOfSize(6)
expect(queryResults).toBeArrayOfSize(6);
queryResults.map((e, idx) => {
expect(e).toMatchObject(TEST_RELATIONS[idx])
})
expect(e).toMatchObject(TEST_RELATIONS[idx]);
});
});

it('should allow filtering on a uni directional many to one relation', async () => {
Expand All @@ -173,10 +172,10 @@ describe('TypeOrmQueryService', (): void => {
},
},
});
expect(queryResults).toBeArrayOfSize(6)
expect(queryResults).toBeArrayOfSize(6);
queryResults.map((e, idx) => {
expect(e).toMatchObject(TEST_RELATIONS[idx])
})
expect(e).toMatchObject(TEST_RELATIONS[idx]);
});
});

it('should allow filtering on a many to one relation with paging', async () => {
Expand All @@ -197,10 +196,10 @@ describe('TypeOrmQueryService', (): void => {
sorting: [{ field: 'testRelationPk', direction: SortDirection.ASC }],
paging: { limit: 3 },
});
expect(queryResults).toBeArrayOfSize(3)
expect(queryResults).toBeArrayOfSize(3);
queryResults.map((e, idx) => {
expect(e).toMatchObject(TEST_RELATIONS[idx])
})
expect(e).toMatchObject(TEST_RELATIONS[idx]);
});
});
});

Expand Down Expand Up @@ -617,7 +616,9 @@ describe('TypeOrmQueryService', (): void => {
it('call select and return the with a uni-directional relation', async () => {
const entity = TEST_ENTITIES[2];
const queryService = moduleRef.get(TestEntityService);
const queryResult = (await queryService.queryRelations(TestRelation, 'manyToManyUniDirectional', entity, {})).map((r) => {
const queryResult = (
await queryService.queryRelations(TestRelation, 'manyToManyUniDirectional', entity, {})
).map((r) => {
delete r.relationOfTestRelationId;
return r;
});
Expand Down
21 changes: 7 additions & 14 deletions packages/query-typeorm/src/query/filter-query.builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,28 +270,21 @@ export class FilterQueryBuilder<Entity> {
}

getReferencedRelationsRecursive(metadata: EntityMetadata, filter: Filter<unknown> = {}): NestedRecord {
const referencedFields = Array.from(new Set(Object.keys(filter)));
const referencedFields = Array.from(new Set(Object.keys(filter) as (keyof Filter<unknown>)[]));
return referencedFields.reduce((prev, curr) => {
if (filter.and) {
for (const andFilter of filter.and) {
prev = merge(prev, this.getReferencedRelationsRecursive(metadata, andFilter));
}
}
if (filter.or) {
for (const orFilter of filter.or) {
prev = merge(prev, this.getReferencedRelationsRecursive(metadata, orFilter));
const currFilterValue = filter[curr];
if ((curr === 'and' || curr === 'or') && currFilterValue) {
for (const subFilter of currFilterValue) {
prev = merge(prev, this.getReferencedRelationsRecursive(metadata, subFilter));
}
}
const referencedRelation = metadata.relations.find((r) => r.propertyName === curr);
if (!referencedRelation) return prev;
return {
...prev,
[curr]: merge(
(prev as Record<string, any>)[curr],
this.getReferencedRelationsRecursive(
referencedRelation.inverseEntityMetadata,
(filter as Record<string, Filter<unknown>>)[curr],
),
(prev as NestedRecord)[curr],
this.getReferencedRelationsRecursive(referencedRelation.inverseEntityMetadata, currFilterValue),
),
};
}, {});
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"noImplicitReturns": true,
"pretty": true,
"strict": true,
"noUnusedLocals": true
"noUnusedLocals": true,
"types": ["jest-extended"]
},
"exclude": ["node_modules"]
}

0 comments on commit 6af8af1

Please sign in to comment.