Skip to content

Commit

Permalink
feat: add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
daveleek committed Sep 9, 2023
1 parent 6ddcde1 commit 299b5a4
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/fetch.cjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
fetch: fetch
}
};
70 changes: 69 additions & 1 deletion test/unleash-action.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { UnleashAction } from '../src/unleash-action';

test('calls home', async () => {
test('checks features', async () => {
const unleash = {};
unleash.isEnabled = () => {
return true;
Expand Down Expand Up @@ -35,3 +35,71 @@ test('calls home', async () => {
await action.run();
expect(resultSet).toBe(true);
});

test('checks variants', async () => {
const unleash = {};
unleash.isEnabled = () => {
return true;
};
unleash.on = () => {};
unleash.start = () => {};
unleash.stop = () => {};
unleash.getVariant = () => {
return {
name: 'variant-1',
enabled: true,
payload: { value: 'red' },
};
};

const metrics = {};
metrics.count = () => {};
metrics.countVariant = () => {};
metrics.sendMetrics = () => {};
let resultSets = [];
const action = new UnleashAction({
client: unleash,
metrics,
url: 'http://localhost:3000',
clientKey: 'client-1',
appName: 'test-app',
environment: 'test',
context: {},
variants: ['variant-1'],
setResult: (name, value) => {
resultSets.push({ name, value });
},
});
await action.run();
expect(resultSets).toEqual(
expect.arrayContaining([
expect.objectContaining({ name: 'variant-1', value: true }),
expect.objectContaining({ name: 'variant-1_variant', value: 'red' }),
])
);
});

test('end calls sendMetrics and stop', async () => {
const unleash = {};
unleash.stop = jest.fn();
unleash.on = () => {};
const metrics = {};
metrics.sendMetrics = jest.fn();
const action = new UnleashAction({
client: unleash,
metrics,
url: 'http://localhost:3000',
clientKey: 'client-1',
appName: 'test-app',
environment: 'test',
context: {},
variants: ['variant-1'],
setResult: (name, value) => {
resultSets.push({ name, value });
},
});

await action.end();
expect(metrics.sendMetrics).toBeCalled();
expect(unleash.stop).toBeCalled();
});

0 comments on commit 299b5a4

Please sign in to comment.