-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add events tests for web-sdk (#410)
- added tests based on [spec PR](open-feature/spec#171) Signed-off-by: RealAnna <[email protected]>
- Loading branch information
Showing
2 changed files
with
160 additions
and
1 deletion.
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 |
---|---|---|
|
@@ -426,4 +426,5 @@ describe('Evaluation details structure', () => { | |
}); | ||
}); | ||
|
||
// context tests are not needed? | ||
|
||
|
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,158 @@ | ||
import { | ||
Provider, | ||
ProviderEvents, | ||
OpenFeatureEventEmitter, | ||
OpenFeatureAPI, | ||
} from '../src'; | ||
|
||
const ERROR_REASON = 'error'; | ||
const ERROR_CODE = 'MOCKED_ERROR'; | ||
|
||
const MOCK_PROVIDER = { | ||
metadata: { | ||
name: 'mock-events', | ||
}, | ||
initialize: jest.fn(() => { | ||
return Promise.resolve(undefined); | ||
}), | ||
events: new OpenFeatureEventEmitter(), | ||
} as unknown as Provider; | ||
|
||
const ERROR_MOCK_PROVIDER = { | ||
metadata: { | ||
name: 'mock-events-failure', | ||
}, | ||
initialize: jest.fn(() => { | ||
return Promise.reject({ | ||
reason: ERROR_REASON, | ||
errorCode: ERROR_CODE, | ||
}); | ||
}), | ||
events: new OpenFeatureEventEmitter(), | ||
} as unknown as Provider; | ||
|
||
|
||
|
||
describe('Events', () => { | ||
// set timeouts short for this suite. | ||
jest.setTimeout(1000); | ||
let API: OpenFeatureAPI = new (OpenFeatureAPI as any)(); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
API = new (OpenFeatureAPI as any)(); //TODO this hacky thing should be removed with provider mapping: https://github.com/open-feature/js-sdk/issues/421 | ||
}); | ||
|
||
describe('Requirement 5.1.1', () => { | ||
it('The provider defines a mechanism for signalling the occurrence of an event`PROVIDER_READY`', (done) => { | ||
|
||
MOCK_PROVIDER.events?.addListener(ProviderEvents.Ready, () => { | ||
try { | ||
expect(API.providerMetadata.name).toBe(MOCK_PROVIDER.metadata.name); | ||
expect(MOCK_PROVIDER.initialize).toHaveBeenCalled(); | ||
done(); | ||
} catch (err) { | ||
done(err); | ||
} | ||
}); | ||
API.setProvider(MOCK_PROVIDER); | ||
}); | ||
|
||
it('It defines a mechanism for signalling `PROVIDER_ERROR`', (done) => { | ||
//make sure an error event is fired when initialize promise reject | ||
ERROR_MOCK_PROVIDER.events?.addListener(ProviderEvents.Error, () => { | ||
try { | ||
expect(API.providerMetadata.name).toBe(ERROR_MOCK_PROVIDER.metadata.name); | ||
expect(ERROR_MOCK_PROVIDER.initialize).toHaveBeenCalled(); | ||
done(); | ||
} catch (err) { | ||
done(err); | ||
} | ||
}); | ||
API.setProvider(ERROR_MOCK_PROVIDER); | ||
}); | ||
|
||
}); | ||
|
||
describe('Requirement 5.2.1, 5.2.3, 5.2.4, 5.2.5 ', () => { | ||
|
||
it('If the provider `initialize` function terminates normally, `PROVIDER_READY` handlers MUST run', (done) => { | ||
const client = API.getClient(); | ||
client.addHandler(ProviderEvents.Ready, () => { | ||
done(); | ||
}); | ||
API.setProvider(MOCK_PROVIDER); | ||
}); | ||
|
||
it('If the provider `initialize` function terminates abnormally, `PROVIDER_ERROR` handlers MUST run.', (done) => { | ||
const client = API.getClient(); | ||
client.addHandler(ProviderEvents.Error, () => { | ||
done(); | ||
}); | ||
API.setProvider(ERROR_MOCK_PROVIDER); | ||
}); | ||
|
||
it('It defines a mechanism for signalling `PROVIDER_CONFIGURATION_CHANGED`', (done) => { | ||
const client = API.getClient(); | ||
client.addHandler(ProviderEvents.ConfigurationChanged, () => { | ||
done(); | ||
}); | ||
API.setProvider(MOCK_PROVIDER); | ||
// emit a change event from the mock provider | ||
MOCK_PROVIDER.events?.emit(ProviderEvents.ConfigurationChanged); | ||
}); | ||
|
||
it('`PROVIDER_READY` handlers added after the provider is already in a ready state MUST run immediately.', (done) => { | ||
const client = API.getClient(); | ||
|
||
MOCK_PROVIDER.events?.addListener(ProviderEvents.Ready, () => { | ||
try { | ||
expect(MOCK_PROVIDER.initialize).toHaveBeenCalled(); | ||
jest.setTimeout(10); | ||
let count = 0; | ||
client.addHandler(ProviderEvents.Ready, () => { | ||
if (count == 0) { | ||
count++; | ||
done(); | ||
} | ||
}); | ||
} catch (err) { | ||
done(err); | ||
} | ||
}); | ||
|
||
API.setProvider(MOCK_PROVIDER); | ||
}); | ||
}); | ||
|
||
describe('Requirement 5.2.6 ', () => { | ||
it('Event handlers MUST persist across `provider` changes.', (done) => { | ||
const client = API.getClient(); | ||
|
||
const myProvider: Provider = { | ||
metadata: { | ||
name: 'first', | ||
}, | ||
initialize: jest.fn(() => { | ||
return Promise.resolve(undefined); | ||
}), | ||
events: new OpenFeatureEventEmitter(), | ||
} as unknown as Provider; | ||
|
||
API.setProvider(myProvider); | ||
let counter = 0; | ||
client.addHandler(ProviderEvents.Ready, () => { | ||
if (API.providerMetadata.name == myProvider.metadata.name) { | ||
API.setProvider(MOCK_PROVIDER); | ||
counter++; | ||
} else { | ||
expect(counter).toBeGreaterThan(0); | ||
expect(API.providerMetadata.name).toBe(MOCK_PROVIDER.metadata.name); | ||
if (counter == 1) { | ||
done(); | ||
} | ||
} | ||
}); | ||
}); | ||
}); | ||
}); |