-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
517fec5
commit aefd05e
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { expect } from 'chai'; | ||
import { configFactory, ConfigFunction } from '../src/configFactory'; | ||
import { Config } from '../src/types'; | ||
|
||
describe('configFactory', () => { | ||
let config: ReturnType<typeof configFactory>; | ||
|
||
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'); | ||
}); | ||
}); |