Skip to content

Commit

Permalink
test: add events tests for web-sdk (#410)
Browse files Browse the repository at this point in the history
- added tests based on [spec
PR](open-feature/spec#171)

Signed-off-by: RealAnna <[email protected]>
  • Loading branch information
RealAnna authored May 5, 2023
1 parent 97fa63d commit 0861e49
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/client/test/client.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,4 +426,5 @@ describe('Evaluation details structure', () => {
});
});

// context tests are not needed?


158 changes: 158 additions & 0 deletions packages/client/test/events.spec.ts
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();
}
}
});
});
});
});

0 comments on commit 0861e49

Please sign in to comment.