Skip to content

Commit

Permalink
Improved watchSnapshot API (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
petruki committed Apr 13, 2024
1 parent 590567a commit ce86994
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/index.js
Expand Up @@ -114,20 +114,18 @@ export class Switcher {
return Switcher.snapshot?.data.domain.version || 0;
}

static watchSnapshot(success, error) {
static watchSnapshot(success = () => {}, error = () => {}) {
if (Switcher.testEnabled || !Switcher.options.snapshotLocation?.length) {
return;
return error(new Error('Watch Snapshot cannot be used in test mode or without a snapshot location'));
}

const snapshotFile = `${Switcher.options.snapshotLocation}${Switcher.context.environment}.json`;
watchFile(snapshotFile, () => {
try {
Switcher.snapshot = loadDomain(Switcher.options.snapshotLocation, Switcher.context.environment);
if (success)
success();
success();
} catch (e) {
if (error)
error(e);
error(e);
}
});
}
Expand Down
5 changes: 5 additions & 0 deletions test/strategy-operations/network.test.js
Expand Up @@ -24,6 +24,11 @@ describe('Processing strategy: NETWORK', () => {
assert.isTrue(result);
});

it('should agree when input range EXIST - Irregular CIDR', async function () {
const result = await processOperation(StrategiesType.NETWORK, OperationsType.EXIST, '10.0.0.3', ['10.0.0.3/24']);
assert.isTrue(result);
});

it('should NOT agree when input range DOES NOT EXIST', async () => {
const result = await processOperation(
StrategiesType.NETWORK, OperationsType.EXIST, '10.0.0.4', mock_values1);
Expand Down
13 changes: 13 additions & 0 deletions test/switcher-watch-snapshot.test.js
Expand Up @@ -106,4 +106,17 @@ describe('E2E test - Switcher local - Watch Snapshot:', function () {
}
});

it('should NOT allow to watch snapshot - Switcher test is enabled', function (done) {
Switcher.setTestEnabled();

let errorMessage;
Switcher.watchSnapshot(undefined, (err) => {
errorMessage = err.message;
});

assert.equal(errorMessage, 'Watch Snapshot cannot be used in test mode or without a snapshot location');
done();
Switcher.setTestDisabled();
});

});

0 comments on commit ce86994

Please sign in to comment.