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, UserInfo} from '../src/utils/utils'; describe('frontend:SomeModule', () => { const CONFIG = {EXTERNAL_BASE_URL: "http://demo.url"}; before(() => { setConfig(CONFIG); fetchMock.config.overwriteRoutes = true; }); it('should return username', async () => { const tests: [Partial, 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|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); } }); });