Skip to content

Commit 2a09f46

Browse files
Merge branch 'zhiyuanliang/select-snapshot' of https://github.com/Azure/AppConfiguration-JavaScriptProvider into zhiyuanliang/select-snapshot
2 parents 501cfe4 + 28c2d0e commit 2a09f46

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export type SettingSelector = {
1717
* For all other cases the characters: asterisk `*`, comma `,`, and backslash `\` are reserved. Reserved characters must be escaped using a backslash (\).
1818
* e.g. the key filter `a\\b\,\*c*` returns all key-values whose key starts with `a\b,*c`.
1919
*/
20-
keyFilter: string,
20+
keyFilter?: string,
2121

2222
/**
2323
* The label filter to apply when querying Azure App Configuration for key-values.

test/load.test.ts

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as chaiAsPromised from "chai-as-promised";
66
chai.use(chaiAsPromised);
77
const expect = chai.expect;
88
import { load } from "./exportedApi.js";
9-
import { MAX_TIME_OUT, mockAppConfigurationClientListConfigurationSettings, restoreMocks, createMockedConnectionString, createMockedEndpoint, createMockedTokenCredential, createMockedKeyValue } from "./utils/testHelper.js";
9+
import { MAX_TIME_OUT, mockAppConfigurationClientListConfigurationSettings, mockAppConfigurationClientGetSnapshot, mockAppConfigurationClientListConfigurationSettingsForSnapshot, restoreMocks, createMockedConnectionString, createMockedEndpoint, createMockedTokenCredential, createMockedKeyValue } from "./utils/testHelper.js";
1010

1111
const mockedKVs = [{
1212
key: "app.settings.fontColor",
@@ -418,4 +418,32 @@ describe("load", function () {
418418
settings.constructConfigurationObject({ separator: "%" });
419419
}).to.throw("Invalid separator '%'. Supported values: '.', ',', ';', '-', '_', '__', '/', ':'.");
420420
});
421+
422+
it("should load from snapshot", async () => {
423+
const snapshotName = "Test";
424+
mockAppConfigurationClientGetSnapshot(snapshotName, {compositionType: "key"});
425+
mockAppConfigurationClientListConfigurationSettingsForSnapshot(snapshotName, [[{key: "TestKey", value: "TestValue"}].map(createMockedKeyValue)]);
426+
const connectionString = createMockedConnectionString();
427+
const settings = await load(connectionString, {
428+
selectors: [{
429+
snapshotName: snapshotName
430+
}]
431+
});
432+
expect(settings).not.undefined;
433+
expect(settings).not.undefined;
434+
expect(settings.get("TestKey")).eq("TestValue");
435+
restoreMocks();
436+
});
437+
438+
it("should throw error when snapshot composition type is not key", async () => {
439+
const snapshotName = "Test";
440+
mockAppConfigurationClientGetSnapshot(snapshotName, {compositionType: "key_label"});
441+
const connectionString = createMockedConnectionString();
442+
expect(load(connectionString, {
443+
selectors: [{
444+
snapshotName: snapshotName
445+
}]
446+
})).eventually.rejectedWith(`Composition type for the selected snapshot with name ${snapshotName} must be 'key'.`);
447+
restoreMocks();
448+
});
421449
});

test/utils/testHelper.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,35 @@ function mockAppConfigurationClientGetConfigurationSetting(kvList, customCallbac
162162
});
163163
}
164164

165+
function mockAppConfigurationClientGetSnapshot(snapshotName: string, mockedResponse: any, customCallback?: (options) => any) {
166+
sinon.stub(AppConfigurationClient.prototype, "getSnapshot").callsFake((name, options) => {
167+
if (customCallback) {
168+
customCallback(options);
169+
}
170+
171+
if (name === snapshotName) {
172+
return mockedResponse;
173+
} else {
174+
throw new RestError("", { statusCode: 404 });
175+
}
176+
});
177+
}
178+
179+
function mockAppConfigurationClientListConfigurationSettingsForSnapshot(snapshotName: string, pages: ConfigurationSetting[][], customCallback?: (options) => any) {
180+
sinon.stub(AppConfigurationClient.prototype, "listConfigurationSettingsForSnapshot").callsFake((name, listOptions) => {
181+
if (customCallback) {
182+
customCallback(listOptions);
183+
}
184+
185+
if (name === snapshotName) {
186+
const kvs = _filterKVs(pages.flat(), listOptions);
187+
return getMockedIterator(pages, kvs, listOptions);
188+
} else {
189+
throw new RestError("", { statusCode: 404 });
190+
}
191+
});
192+
}
193+
165194
// uriValueList: [["<secretUri>", "value"], ...]
166195
function mockSecretClientGetSecret(uriValueList: [string, string][]) {
167196
const dict = new Map();
@@ -265,6 +294,8 @@ export {
265294
sinon,
266295
mockAppConfigurationClientListConfigurationSettings,
267296
mockAppConfigurationClientGetConfigurationSetting,
297+
mockAppConfigurationClientGetSnapshot,
298+
mockAppConfigurationClientListConfigurationSettingsForSnapshot,
268299
mockAppConfigurationClientLoadBalanceMode,
269300
mockConfigurationManagerGetClients,
270301
mockSecretClientGetSecret,

0 commit comments

Comments
 (0)