From aefd05e4f6026c23746779887ff7af404a80e505 Mon Sep 17 00:00:00 2001 From: kushalshit27 <43465488+kushalshit27@users.noreply.github.com> Date: Tue, 4 Feb 2025 14:23:28 +0530 Subject: [PATCH] update unit test --- test/configFactory.test.ts | 41 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 test/configFactory.test.ts diff --git a/test/configFactory.test.ts b/test/configFactory.test.ts new file mode 100644 index 00000000..9d77332e --- /dev/null +++ b/test/configFactory.test.ts @@ -0,0 +1,41 @@ +import { expect } from 'chai'; +import { configFactory, ConfigFunction } from '../src/configFactory'; +import { Config } from '../src/types'; + +describe('configFactory', () => { + let config: ReturnType; + + beforeEach(() => { + config = configFactory(); + }); + + it('should set and get configuration values', () => { + config.setValue('someKey' as keyof Config, 'someValue'); + expect(config('someKey' as keyof Config)).to.equal('someValue'); + }); + + it('should throw an error if no provider is set and key is not in settings', () => { + expect(() => config('someKey' as keyof Config)).to.throw( + 'A configuration provider has not been set' + ); + }); + + it('should use the provider function to get configuration values', () => { + const providerFunction: ConfigFunction = (key) => { + if ((key as string) === 'someKey') return 'providedValue'; + return null; + }; + config.setProvider(providerFunction); + expect(config('someKey' as keyof Config)).to.equal('providedValue'); + }); + + it('should prioritize settings over provider function', () => { + config.setValue('someKey' as keyof Config, 'someValue'); + const providerFunction: ConfigFunction = (key) => { + if ((key as string) === 'someKey') return 'providedValue'; + return null; + }; + config.setProvider(providerFunction); + expect(config('someKey' as keyof Config)).to.equal('someValue'); + }); +});