Skip to content

Instantly share code, notes, and snippets.

View rjpkuyper's full-sized avatar

Robert-Jan Kuyper rjpkuyper

View GitHub Profile
import { createHttpGet, createUrl, Paths } from './get-env-vars'
describe("GetProducts", () => {
const products = [{ id: 1 }];
const url = 'https://example.com'
const httpGet = jest.fn().mockResolvedValue({
json: jest.fn().mockResolvedValue(products)
});
export enum Paths {
PRODUCTS = '/products'
}
export const createLogger = (logger) => {
error: e => logger.error(`Something bad happened today: ${e}`)
}
export const createUrl = (path: Paths, url = process.env.BASE_URL) => `${url}${path}`
import { createUrl, getProducts, Paths } from './get-env-vars'
import * as fns from './get-env-vars'
describe("GetEnvVars", () => {
const MOCKED_PRODCUTS = [{ id: 1 }];
global.fetch = jest.fn(() => Promise.resolve({
json: () => Promise.resolve(MOCKED_PRODCUTS)
}));
export enum Paths {
PRODUCTS = '/products'
}
export const createLogger = () => {
error: e => console.error(`Something bad happened today: ${e}!`)
}
export const loggerService = createLogger()
import { createUrl, getProducts, Paths } from './get-env-vars'
describe("GetProducts", () => {
const products = [{ id: 1 }];
const httpGet = jest.fn().mockResolvedValue({
json: jest.fn().mockResolvedValue(products)
});
export enum Paths {
PRODUCTS = '/products'
}
export const createUrl = (path: Paths, url = process.env.BASE_URL) => `${url}${path}`
export const getProducts = (
httpGet = fetch,
url: string = createUrl(Paths.PRODUCTS),
) => httpGet(url).then(e => e.json())
import { createUrl, getProducts, Paths } from './get-env-vars'
describe("GetEnvVars", () => {
const MOCKED_PRODUCTS = [{ id: 1 }];
global.fetch = jest.fn(() => Promise.resolve({
json: () => Promise.resolve(MOCKED_PRODUCTS)
}));
export enum Paths {
PRODUCTS = '/products'
}
export const createUrl = (path: Paths) => `${process.env.BASE_URL}${path}`
export const productsUrl = createUrl(Paths.PRODUCTS)
export const getProducts = () => fetch(productsUrl).then(e => e.json())
import { createUrl, Paths } from './get-env-vars'
describe("GetEnvVars", () => {
test('should create a new url', () => {
const url = 'https://example.com'
expect(createUrl(Paths.PRODUCTS, url)).toEqual(`${url}/${Paths.PRODUCTS}`);
});
})
@rjpkuyper
rjpkuyper / get-env-vars.ts
Created February 17, 2023 06:06
An updated version of the original
export enum Paths {
PRODUCTS = '/products'
}
// pass the dependency, simply as optional variable
export const createUrl = (path: Paths, baseUrl = process.env.BASE_URL) => `${url}${path}`
export const productsUrl = createUrl(Paths.PRODUCTS)