UNPKG

@pallad/app-env

Version:

Detects environment (production, staging, test, development, ci) and helps making decision based on that

127 lines (126 loc) 4.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const conditional_type_checks_1 = require("conditional-type-checks"); const setup_1 = require("../setup"); const Builder_1 = require("../Builder"); describe('Builder', () => { const PRODUCTION = setup_1.factory.create('production'); const DEVELOPMENT = setup_1.factory.create('development'); const TEST = setup_1.factory.create('test'); const CI = setup_1.factory.create('ci'); const STAGING = setup_1.factory.create('staging'); const PREVIEW = setup_1.factory.create('preview'); const VALUE = 'foo'; const VALUE_OTHER = 'bar'; it('forNames', () => { expect(new Builder_1.Builder(PRODUCTION) .forEnv(['development', 'production'], VALUE) .get()).toEqual(VALUE); }); it('builder returns first matching result', () => { expect(new Builder_1.Builder(PRODUCTION) .forStaging(VALUE_OTHER) .forEnv(['production'], VALUE) .forProduction('what?') .get()).toEqual(VALUE); }); it('default returned if none matches', () => { expect(new Builder_1.Builder(PRODUCTION) .forStaging(VALUE) .forTest(VALUE) .getOrDefault(VALUE_OTHER)).toEqual(VALUE_OTHER); expect(new Builder_1.Builder(PRODUCTION) .forStaging(VALUE) .forTest(VALUE) .forProduction(VALUE) .getOrDefault(VALUE_OTHER)).toEqual(VALUE); }); it('forProduction', () => { expect(new Builder_1.Builder(PRODUCTION) .forProduction(VALUE) .get()).toEqual(VALUE); expect(new Builder_1.Builder(PRODUCTION) .forDevelopment(VALUE) .forStaging(VALUE) .forTest(VALUE) .forPreview(VALUE) .forCI(VALUE) .getOrDefault(VALUE_OTHER)).toEqual(VALUE_OTHER); }); it('forDevelopment', () => { expect(new Builder_1.Builder(DEVELOPMENT) .forDevelopment(VALUE) .get()).toEqual(VALUE); expect(new Builder_1.Builder(DEVELOPMENT) .forProduction(VALUE) .forStaging(VALUE) .forTest(VALUE) .forPreview(VALUE) .forCI(VALUE) .getOrDefault(VALUE_OTHER)).toEqual(VALUE_OTHER); }); it('forStaging', () => { expect(new Builder_1.Builder(STAGING) .forStaging(VALUE) .get()).toEqual(VALUE); expect(new Builder_1.Builder(STAGING) .forDevelopment(VALUE) .forProduction(VALUE) .forTest(VALUE) .forPreview(VALUE) .forCI(VALUE) .getOrDefault(VALUE_OTHER)).toEqual(VALUE_OTHER); }); it('forTest', () => { expect(new Builder_1.Builder(TEST) .forTest(VALUE) .get()).toEqual(VALUE); expect(new Builder_1.Builder(TEST) .forDevelopment(VALUE) .forProduction(VALUE) .forStaging(VALUE) .forPreview(VALUE) .forCI(VALUE) .getOrDefault(VALUE_OTHER)).toEqual(VALUE_OTHER); }); it('forCI', () => { expect(new Builder_1.Builder(CI) .forCI(VALUE) .get()).toEqual(VALUE); expect(new Builder_1.Builder(CI) .forDevelopment(VALUE) .forProduction(VALUE) .forStaging(VALUE) .forPreview(VALUE) .forTest(VALUE) .getOrDefault(VALUE_OTHER)).toEqual(VALUE_OTHER); }); it('forPreview', () => { expect(new Builder_1.Builder(PREVIEW) .forPreview(VALUE) .get()).toEqual(VALUE); expect(new Builder_1.Builder(PREVIEW) .forDevelopment(VALUE) .forProduction(VALUE) .forStaging(VALUE) .forTest(VALUE) .forCI(VALUE) .getOrDefault(VALUE_OTHER)).toEqual(VALUE_OTHER); }); describe('types', () => { it('simple type inference', () => { const value = new Builder_1.Builder({}) .forTest('bar') .forProduction('foo') .get(); (0, conditional_type_checks_1.assert)(true); }); it('providing default removed possibility of undefined', () => { const value = new Builder_1.Builder({}) .forTest('bar') .forProduction('foo') .getOrDefault('what?'); (0, conditional_type_checks_1.assert)(true); }); }); });