Skip to content

Commit

Permalink
Finish lib typing
Browse files Browse the repository at this point in the history
  • Loading branch information
Pierre-Eric Garcia committed Aug 30, 2023
1 parent 5a75e51 commit c817f59
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 149 deletions.
110 changes: 78 additions & 32 deletions frontend/package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@
"@justfixnyc/react-aria-modal": "^5.1.7-alpha.0",
"@sentry/react": "^7.17.4",
"@tanstack/react-table": "^8.5.15",
"@types/jest": "^29.5.1",
"@types/lodash": "^4.14.192",
"@types/node": "^20.1.5",
"@types/lodash": "^4.14.197",
"@types/react": "^18.2.6",
"@types/react-dom": "^18.0.11",
"@types/react-router-dom": "^5.3.3",
Expand Down Expand Up @@ -64,6 +62,8 @@
"@testing-library/react-hooks": "^8.0.1",
"@types/axios": "^0.14.0",
"@types/flat": "^5.0.2",
"@types/jest": "^29.5.4",
"@types/node": "^20.5.7",
"http-proxy-middleware": "^2.0.6",
"jest-mock-extended": "^3.0.4",
"nock": "^13.3.0",
Expand Down
1 change: 1 addition & 0 deletions frontend/src/components/templates/Stats.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ export const Stats = () => {
useEffect(() => {
async function fetchStats() {
const result = await getTargetAPIList(targetApi);
console.log(result.data.enrollment_by_target_api);

setStats({
...result.data,
Expand Down
70 changes: 40 additions & 30 deletions frontend/src/lib/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,21 @@ import {
} from './index';
import groupEmailAddresses from '../../mock/group_email_addresses_samples.json';
import { transform } from 'lodash';
import { Enrollment } from '../components/templates/InstructorEnrollmentList';
import { DataProviderConfiguration } from '../config/data-provider-configurations';

describe('utils', () => {
describe('getErrorMessages', () => {
it('should return proper error message for error from nginx', () => {
const errorObject: NetworkError = {
const errorObject = {
response: {
data: '<html>\r\n<head><title>502 Bad Gateway</title></head>\r\n<body bgcolor="white">\r\n<center><h1>502 Bad Gateway</h1></center>\r\n<hr><center>nginx/1.10.3 (Ubuntu)</center>\r\n</body>\r\n</html>\r\n<!-- a padding to disable MSIE and Chrome friendly error page -->\r\n<!-- a padding to disable MSIE and Chrome friendly error page -->\r\n<!-- a padding to disable MSIE and Chrome friendly error page -->\r\n<!-- a padding to disable MSIE and Chrome friendly error page -->\r\n<!-- a padding to disable MSIE and Chrome friendly error page -->\r\n<!-- a padding to disable MSIE and Chrome friendly error page -->\r\n',
status: 502,
statusText: 'Bad Gateway',
},
};

expect(getErrorMessages(errorObject)).toEqual([
expect(getErrorMessages(errorObject as NetworkError)).toEqual([
'Une erreur est survenue. Le code de l’erreur est 502 (Bad Gateway). Merci de réessayer ultérieurement. Vous pouvez également nous signaler cette erreur par mail à [email protected].',
]);
});
Expand All @@ -53,7 +55,7 @@ describe('utils', () => {
},
};

expect(getErrorMessages(errorObject)).toEqual([
expect(getErrorMessages(errorObject as NetworkError)).toEqual([
'Vous devez renseigner la description de la démarche avant de continuer',
'Vous devez renseigner un prénom pour le contact technique avant de continuer',
'Vous devez renseigner un nom pour le contact technique avant de continuer',
Expand All @@ -65,7 +67,7 @@ describe('utils', () => {
message: 'Network Error',
};

expect(getErrorMessages(errorObject)).toEqual([
expect(getErrorMessages(errorObject as NetworkError)).toEqual([
'Une erreur de connexion au serveur est survenue. Merci de vérifier que vous êtes bien connecté à internet. Si vous utilisez un réseau d’entreprise, merci de signaler cette erreur à l’administrateur de votre réseau informatique. Si le problème persiste, vous pouvez nous contacter par mail à [email protected].',
]);
});
Expand All @@ -82,7 +84,7 @@ describe('utils', () => {
},
};

expect(getErrorMessages(errorObject)).toEqual([
expect(getErrorMessages(errorObject as NetworkError)).toEqual([
'La validation a échoué : Copied from enrollment n’est pas disponible',
]);
});
Expand All @@ -98,7 +100,7 @@ describe('utils', () => {
},
};

expect(getErrorMessages(errorObject)).toEqual([
expect(getErrorMessages(errorObject as NetworkError)).toEqual([
'Vous n’êtes pas autorisé à modifier cette ressource',
]);
});
Expand All @@ -115,7 +117,7 @@ describe('utils', () => {
},
};

expect(getErrorMessages(errorObject)).toEqual([
expect(getErrorMessages(errorObject as NetworkError)).toEqual([
'Vous devez vous connecter ou vous inscrire pour continuer.',
]);
});
Expand Down Expand Up @@ -273,7 +275,7 @@ describe('utils', () => {
const diff = {
updated_at: ['2021-12-28T15:51:35.552Z', '2021-12-28T15:51:35.565Z'],
};
const changelog = [];
const changelog: [] = [];
expect(getChangelog(diff)).toEqual(changelog);
});
});
Expand Down Expand Up @@ -490,7 +492,7 @@ describe('utils', () => {
});

it('should return empty object for undefined', () => {
expect(collectionWithKeyToObject(undefined)).toStrictEqual({});
expect(collectionWithKeyToObject(undefined as any)).toStrictEqual({});
});

it('should turn collection with key into object', () => {
Expand All @@ -506,21 +508,23 @@ describe('utils', () => {
});

describe('getStateFromUrlParams', () => {
let location = null;
let originalWindow: typeof window;

beforeEach(() => {
location = global.window.location;
delete global.window.location;
global.window = Object.create(window);
global.window.location = {
protocol: 'http:',
hostname: 'localhost',
};
originalWindow = window;

(global as any).window = Object.create(window);
Object.defineProperty(window, 'location', {
value: {
protocol: 'http:',
hostname: 'localhost',
},
writable: true,
});
});

afterEach(() => {
global.window.location = location;
location = null;
(global as any).window = originalWindow;
});

it('should return a hash from filtered enrollment list url', () => {
Expand Down Expand Up @@ -725,7 +729,7 @@ describe('utils', () => {
*/
describe('isEmailValid', () => {
it('should return false for undefined value', () => {
expect(isEmailValid(undefined)).toStrictEqual(false);
expect(isEmailValid(undefined as any)).toStrictEqual(false);
});

it('should return false for empty string', () => {
Expand Down Expand Up @@ -787,10 +791,11 @@ describe('utils', () => {

describe('isValidPhoneNumber', () => {
it('should return false for empty phone number', () => {
expect(isValidPhoneNumber()).toStrictEqual(false);
expect(isValidPhoneNumber(undefined as any)).toStrictEqual(false);
});

it('should return false for non string value', () => {
expect(isValidPhoneNumber([])).toStrictEqual(false);
expect(isValidPhoneNumber([] as any)).toStrictEqual(false);
});
it('should return true for valid phone number no spaces', () => {
expect(isValidPhoneNumber('0123456789')).toStrictEqual(true);
Expand All @@ -805,10 +810,10 @@ describe('utils', () => {

describe('isIndividualEmailAddress', () => {
it('should return false for empty phone number', () => {
expect(isIndividualEmailAddress()).toStrictEqual(false);
expect(isIndividualEmailAddress(undefined as any)).toStrictEqual(false);
});
it('should return false for non string value', () => {
expect(isIndividualEmailAddress([])).toStrictEqual(false);
expect(isIndividualEmailAddress([] as any)).toStrictEqual(false);
});
it('should return true for individual email address', () => {
expect(
Expand All @@ -832,10 +837,10 @@ describe('utils', () => {

describe('isValidMobilePhoneNumber', () => {
it('should return false for empty phone number', () => {
expect(isValidMobilePhoneNumber()).toStrictEqual(false);
expect(isValidMobilePhoneNumber(undefined as any)).toStrictEqual(false);
});
it('should return false for non string value', () => {
expect(isValidMobilePhoneNumber([])).toStrictEqual(false);
expect(isValidMobilePhoneNumber([] as any)).toStrictEqual(false);
});
it('should return false for valid phone number no spaces', () => {
expect(isValidMobilePhoneNumber('0123456789')).toStrictEqual(false);
Expand Down Expand Up @@ -935,7 +940,9 @@ describe('utils', () => {
{ label: 'API 2', email: '[email protected]' },
];

const result = dataProviderConfigurationsToContactInfo(parameters);
const result = dataProviderConfigurationsToContactInfo(
parameters as unknown as { [k: string]: DataProviderConfiguration }
);

expect(result).toStrictEqual(expected);
});
Expand All @@ -962,7 +969,9 @@ describe('utils', () => {
{ label: 'API 1, API 2, API 3, API 4, etc.', email: '[email protected]' },
];

const result = dataProviderConfigurationsToContactInfo(parameters);
const result = dataProviderConfigurationsToContactInfo(
parameters as unknown as { [k: string]: DataProviderConfiguration }
);

expect(result).toStrictEqual(expected);
});
Expand Down Expand Up @@ -991,8 +1000,9 @@ describe('utils', () => {
},
},
];

const result = getScopesFromEnrollments(enrollments);
const result = getScopesFromEnrollments(
enrollments as unknown as Enrollment[]
);

expect(result).toStrictEqual(['scope1', 'scope3']);
});
Expand Down
Loading

0 comments on commit c817f59

Please sign in to comment.