Skip to content

Commit

Permalink
[E4E-30]: Fix Response name generation + better tests
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCleric committed Nov 24, 2021
1 parent 55ba637 commit e66d290
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ module.exports = {
},
globals: {
'ts-jest': {
diagnostics: false,
diagnostics: true,
tsconfig: './tsconfig.json',
},
},
Expand Down
19 changes: 10 additions & 9 deletions lib/generateRoutes/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,17 @@ function deleteBuildFolder(): void {
}
}

function doTestImport(tsData: string): unknown {
function doTestImport(tsData: string): void {
const filename = `${randomUUID()}.ts`;

fs.writeFileSync(path.join(buildFolder, filename), tsData);

// eslint-disable-next-line max-len
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, security/detect-non-literal-require, import/no-dynamic-require, global-require, @typescript-eslint/no-var-requires
const endpoints = require(`../../build/tests/${filename}`);
expect(endpoints).toBeTruthy(); // Probably a better test here, but I don't know what it would be
return endpoints;
expect(() => {
// eslint-disable-next-line max-len
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, security/detect-non-literal-require, import/no-dynamic-require, global-require, @typescript-eslint/no-var-requires
const endpoints = require(`../../build/tests/${filename}`);
expect(endpoints).toBeTruthy(); // Probably a better test here, but I don't know what it would be
}).not.toThrow();
}

describe('generateTypescript', () => {
Expand All @@ -47,7 +48,7 @@ describe('generateTypescript', () => {

const typeImport = await generateTypes(apiPath, path.join(buildFolder, `${randomUUID()}_types.ts`));

const tsData = generateTypescript(api, typeImport).replace('apollo-rest-utils', '../../lib');
const tsData = generateTypescript(api, typeImport).replace(/apollo-rest-utils/g, '../../lib');

expect(tsData).toBeTruthy(); // Not empty, null, or undefined

Expand All @@ -70,8 +71,8 @@ describe('normalizeName', () => {
describe('pathToType', () => {
it.each([
['/login', 'LoginResponse'],
['/high-schools/search', 'SearchResponse'],
['/invitations/{studentKey}/users/{mentorEmail}', 'UsersResponse'],
['/high-schools/search', 'HighSchoolsSearchResponse'],
['/invitations/{studentKey}/users/{mentorEmail}', 'InvitationsByStudentkeyUsersByMentoremailResponse'],
])('path %s produces type name %s', (pathName, expectedTypeName) => {
expect(pathToType(pathName)).toEqual(expectedTypeName);
});
Expand Down
2 changes: 1 addition & 1 deletion lib/generateRoutes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export function normalizeName(name: string): string {
}

export function pathToType(endpointPath: string, isArray = false): string {
const result = `${_.camelCase(_.last(endpointPath.split('/').filter(x => !x.startsWith('{'))))}Response`;
const result = `${_.camelCase(normalizeName(endpointPath))}Response`;
return `${isArray ? '[' : ''}${result.replace(result[0], result[0].toUpperCase())}${isArray ? ']' : ''}`;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/useRestQuery/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ describe('useRestQuery Library', () => {
const wrappedRestQuery = wrapRestClientQuery<'refreshToken'>();

const { data } = await wrappedRestQuery({
client: clientMock as unknown as ApolloClient<unknown>,
client: clientMock as unknown as ApolloClient<object>,
endpoint: dummyEndpoint,
query: gql`
query TestClientQuery($input: input) {
Expand Down

0 comments on commit e66d290

Please sign in to comment.