Files
node-template-express/public/js-source/test/SomeModule.ts
2020-11-24 00:20:56 +01:00

59 lines
1.6 KiB
TypeScript

import {describe, it} from 'mocha';
import {expect} from 'chai';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
import * as fetchMock from 'fetch-mock';
import {getUserName} from '../src/SomeModule';
import {setConfig} from '../src/utils/utils';
import {UserInfo} from '../src/utils/userinfo';
describe('frontend:SomeModule', () => {
const CONFIG = {EXTERNAL_BASE_URL: 'http://demo.url'};
beforeEach(() => {
setConfig(CONFIG);
fetchMock.config.overwriteRoutes = true;
});
it('should return username', async () => {
const tests: [Partial<UserInfo>, string][] = [
[
{name: 'John Doe'},
'John Doe',
], [
{name: 'John Doe', email: 'some.mail@example.com'},
'John Doe',
], [
{name: '', email: 'some.mail@example.com'},
'',
],
];
for (const [USER_INFO, RESULT] of tests) {
fetchMock.mock('http://demo.url/api/user', {
status: 200,
body: JSON.stringify(USER_INFO),
});
const name = await getUserName();
expect(name).to.deep.equal(RESULT);
}
});
it('should return default string', async () => {
const RESULT = 'No name found!';
const tests: (Partial<UserInfo>|unknown)[] = [
// eslint-disable-next-line no-null/no-null
{name: null},
// eslint-disable-next-line no-null/no-null
null,
{email: 'some.mail@example.com'},
{name: undefined},
{},
];
for (const USER_INFO of tests) {
fetchMock.mock('http://demo.url/api/user', {
status: 200,
body: JSON.stringify(USER_INFO),
});
const name = await getUserName();
expect(name).to.deep.equal(RESULT);
}
});
});