From 61a400fbd861789938258206f3197ae025c8ddfc Mon Sep 17 00:00:00 2001 From: Thomas Klein Date: Fri, 4 Oct 2019 14:56:20 +0200 Subject: [PATCH] chore: added typings (#157) --- jest/unit/components/__mocks__/token.ts | 5 +++++ package.json | 2 ++ src/utils/login.test.ts | 20 +++++++++++++------- src/utils/login.ts | 2 +- src/utils/storage.ts | 2 +- types/custom.d.ts | 5 +++++ yarn.lock | 10 ++++++++++ 7 files changed, 37 insertions(+), 9 deletions(-) diff --git a/jest/unit/components/__mocks__/token.ts b/jest/unit/components/__mocks__/token.ts index 0f350c8..bdac6fa 100644 --- a/jest/unit/components/__mocks__/token.ts +++ b/jest/unit/components/__mocks__/token.ts @@ -15,6 +15,11 @@ export function generateTokenWithTimeRange(limit = 0) { export function generateTokenWithExpirationAsString() { const payload = { username: 'verdaccio', exp: 'I am not a number' }; + return `xxxxxx.${Base64.encode(JSON.stringify(payload))}.xxxxxx`; +} + +export function generateInvalidToken() { + const payload = `invalidtoken`; return `xxxxxx.${Base64.encode(payload)}.xxxxxx`; } diff --git a/package.json b/package.json index 882d4dd..d1e2632 100644 --- a/package.json +++ b/package.json @@ -19,8 +19,10 @@ "@material-ui/icons": "4.4.3", "@octokit/rest": "16.28.7", "@testing-library/react": "9.2.0", + "@types/autosuggest-highlight": "3.1.0", "@types/enzyme": "3.10.3", "@types/jest": "24.0.18", + "@types/js-base64": "2.3.1", "@types/lodash": "4.14.141", "@types/node": "12.7.8", "@types/react": "16.9.2", diff --git a/src/utils/login.test.ts b/src/utils/login.test.ts index 00e278f..981d41c 100644 --- a/src/utils/login.test.ts +++ b/src/utils/login.test.ts @@ -1,6 +1,11 @@ import { isTokenExpire, makeLogin } from './login'; -import { generateTokenWithTimeRange, generateTokenWithExpirationAsString, generateTokenWithOutExpiration } from '../../jest/unit/components/__mocks__/token'; +import { + generateTokenWithTimeRange, + generateTokenWithExpirationAsString, + generateTokenWithOutExpiration, + generateInvalidToken, +} from '../../jest/unit/components/__mocks__/token'; /* eslint-disable no-console */ console.error = jest.fn(); @@ -9,10 +14,6 @@ jest.mock('./api', () => ({ })); describe('isTokenExpire', (): void => { - test('isTokenExpire - token is not present', () => { - expect(isTokenExpire()).toBeTruthy(); - }); - test('isTokenExpire - token is not a valid payload', (): void => { expect(isTokenExpire('not_a_valid_token')).toBeTruthy(); }); @@ -33,11 +34,16 @@ describe('isTokenExpire', (): void => { }); test('isTokenExpire - token is not a valid json token', (): void => { - const token = generateTokenWithExpirationAsString(); - const result = ['Invalid token:', new SyntaxError('Unexpected token o in JSON at position 1'), 'xxxxxx.W29iamVjdCBPYmplY3Rd.xxxxxx']; + const token = generateInvalidToken(); + const result = ['Invalid token:', new SyntaxError('Unexpected token i in JSON at position 0'), 'xxxxxx.aW52YWxpZHRva2Vu.xxxxxx']; expect(isTokenExpire(token)).toBeTruthy(); expect(console.error).toHaveBeenCalledWith(...result); }); + + test('isTokenExpire - token expiration is not a number', (): void => { + const token = generateTokenWithExpirationAsString(); + expect(isTokenExpire(token)).toBeTruthy(); + }); }); describe('makeLogin', (): void => { diff --git a/src/utils/login.ts b/src/utils/login.ts index df4c0e7..a179e05 100644 --- a/src/utils/login.ts +++ b/src/utils/login.ts @@ -9,7 +9,7 @@ interface PayloadInterface { exp: number; } -export function isTokenExpire(token?: string): boolean { +export function isTokenExpire(token: string | null): boolean { if (!isString(token)) { return true; } diff --git a/src/utils/storage.ts b/src/utils/storage.ts index cd63fdd..3902703 100644 --- a/src/utils/storage.ts +++ b/src/utils/storage.ts @@ -1,6 +1,6 @@ import memoryStorage from 'localstorage-memory'; -let storage; +let storage: Storage; try { localStorage.setItem('__TEST__', ''); localStorage.removeItem('__TEST__'); diff --git a/types/custom.d.ts b/types/custom.d.ts index bf77f9b..437f023 100644 --- a/types/custom.d.ts +++ b/types/custom.d.ts @@ -5,3 +5,8 @@ declare module '*.svg' { } declare module '*.png'; + +declare module 'localstorage-memory' { + const memoryStorage: Storage; + export default memoryStorage; +} diff --git a/yarn.lock b/yarn.lock index 8a4e991..08fb550 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1498,6 +1498,11 @@ "@testing-library/dom" "^6.3.0" "@types/testing-library__react" "^9.1.0" +"@types/autosuggest-highlight@3.1.0": + version "3.1.0" + resolved "https://registry.yarnpkg.com/@types/autosuggest-highlight/-/autosuggest-highlight-3.1.0.tgz#62045417c2f6d841f9f12e715336218e4db0fcb1" + integrity sha512-rJWvrDFKf2NkB3CkeJPcnX3oD0XAQriJOhYC5DUwiK6X/KTM96e1rW+1/PgI0U4tL6GQ2K7dvoiYFhMnAaNLUw== + "@types/babel__core@^7.1.0": version "7.1.3" resolved "https://registry.verdaccio.org/@types%2fbabel__core/-/babel__core-7.1.3.tgz#e441ea7df63cd080dfcd02ab199e6d16a735fc30" @@ -1607,6 +1612,11 @@ resolved "https://registry.verdaccio.org/@types%2fjest/-/jest-23.3.14.tgz#37daaf78069e7948520474c87b80092ea912520a" integrity sha512-Q5hTcfdudEL2yOmluA1zaSyPbzWPmJ3XfSWeP3RyoYvS9hnje1ZyagrZOuQ6+1nQC1Gw+7gap3pLNL3xL6UBug== +"@types/js-base64@2.3.1": + version "2.3.1" + resolved "https://registry.yarnpkg.com/@types/js-base64/-/js-base64-2.3.1.tgz#c39f14f129408a3d96a1105a650d8b2b6eeb4168" + integrity sha512-4RKbhIDGC87s4EBy2Cp2/5S2O6kmCRcZnD5KRCq1q9z2GhBte1+BdsfVKCpG8yKpDGNyEE2G6IqFIh6W2YwWPA== + "@types/json-schema@^7.0.3": version "7.0.3" resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.3.tgz#bdfd69d61e464dcc81b25159c270d75a73c1a636"