Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added BotState.delete() method and tests #679

Merged
merged 2 commits into from
Dec 4, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions libraries/botbuilder-core/src/botState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,26 @@ export class BotState implements PropertyManager {
return Promise.resolve();
}

/**
* Delete the backing state object for the current turn.
*
* @remarks
* The state object will be removed from storage if it exists. If the state object has been
* read in and cached, the cache will be cleared.
*
* ```JavaScript
* await botState.delete(context);
* ```
* @param context Context for current turn of conversation with the user.
*/
public delete(context: TurnContext): Promise<void> {
if (context.turnState.has(this.stateKey)) {
context.turnState.delete(this.stateKey);
}

return Promise.resolve(this.storageKey(context)).then((key: string) => this.storage.delete([key]));
}

/**
* Returns a cached state object or undefined if not cached.
*
Expand Down
11 changes: 11 additions & 0 deletions libraries/botbuilder-core/tests/botState.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,20 @@ describe(`BotState`, function () {
await botState.saveChanges(context);

const items = await storage.read([storageKey]);
assert(items[storageKey], `state removed from storage.`);
assert(!items[storageKey].hasOwnProperty('test'), `state not cleared from storage.`);
});

it(`should delete() state storage.`, async function () {
await botState.load(context);
assert(cachedState(context, botState.stateKey), `invalid initial state`);
await botState.delete(context);
assert(!cachedState(context, botState.stateKey), `state not cleared on context.`);

const items = await storage.read([storageKey]);
assert(!items.hasOwnProperty(storageKey), `state not removed from storage.`);
});

it(`should force immediate saveChanges() of state to storage.`, function (done) {
botState.load(context).then(() => {
const state = cachedState(context, botState.stateKey);
Expand Down