I'm (very) new to writing unit tests and I'm wondering how I should approach testing a static method that constructs our API endpoints based on what the properties are set to in our environment.ts
file.
The environment.ts
file changes with the environment, so I'm curious how I would accommodate for that in my test.
Does my method need to be refactored to make this easier to test? For example, instead of implicitly referencing environment.endpoint, instead pass-in environment as a argument? Would I mock the environment.ts file?
Any suggestions would be helpful.
import {environment} from '../../environments/environment';
export class Utilities {
public static constructAPIEndpoint(): string {
const hostname: string = environment.endpoint;
const protocol: string = environment.local ? 'http:' : 'https:';
return `${protocol}//${hostname}/`;
}
}
environments.ts
export const environment = {
production: false,
local: false,
hostname: 'localhost',
showLogs: true,
endpoint: 'foo-dev-graphapp.com',
appInsights: {
instrumentationKey: '123'
}
};
jasmine test:
import {environment} from '../../environments/environment';
it('constructAPIEndpoint() it should construct an endpoint based on environment.ts', () => {
const endpoint: string = Utilities.constructAPIEndpoint();
/// THIS DOESN'T SEEM - having to recreate logic inside constructAPIEndpoint()
const protocol: string = environment.local ? 'http:' : 'https';
expect(endpoint).toEqual(`${protocol}//${environment.endpoint}`);
});