Skip to content

Commit a331e98

Browse files
add testcase
1 parent 1f41e0d commit a331e98

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-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: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ 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";
10+
import { mock } from "node:test";
1011

1112
const mockedKVs = [{
1213
key: "app.settings.fontColor",
@@ -418,4 +419,19 @@ describe("load", function () {
418419
settings.constructConfigurationObject({ separator: "%" });
419420
}).to.throw("Invalid separator '%'. Supported values: '.', ',', ';', '-', '_', '__', '/', ':'.");
420421
});
422+
423+
it("should load from snapshot", async () => {
424+
const snapshotName = "Test";
425+
mockAppConfigurationClientGetSnapshot(snapshotName, {compositionType: "key"});
426+
mockAppConfigurationClientListConfigurationSettingsForSnapshot(snapshotName, [[{key: "TestKey", value: "TestValue"}].map(createMockedKeyValue)]);
427+
const connectionString = createMockedConnectionString();
428+
const settings = await load(connectionString, {
429+
selectors: [{
430+
snapshotName: snapshotName
431+
}]
432+
});
433+
expect(settings).not.undefined;
434+
expect(settings).not.undefined;
435+
expect(settings.get("TestKey")).eq("TestValue");
436+
});
421437
});

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)