Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions src/AsyncStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ interface AsyncStore {
isInitialized: () => boolean;
getId: () => string | undefined;
getShortId: () => string | undefined;
reset: () => void;
del: (key: string) => void;
Comment on lines +16 to +17
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samirsilwal Please update the README as well.

}

export default AsyncStore;
27 changes: 27 additions & 0 deletions src/impl/domain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,33 @@ export function set(properties: any) {
throw new Error('Invalid arguments provided for asyncStore.set()');
}

/**
* Reset the store by removing all values or a specific value by key.
*/
export function reset() {
logDomain('Resetting the domain store.');

const activeDomain = getActiveDomain();
activeDomain[STORE_KEY] = null;
}

/**
* Delete a key from the store.
*
* @param {string} key
*/
export function del(key: string) {
logDomain(`Deleting ${key} from the domain store.`);

const store = getStore();

if (!store) {
return;
}

delete store[key];
}

/**
* Get a value by a key from the store.
* Throws an error if anything fails while getting the value.
Expand Down
29 changes: 29 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,35 @@ export function initialize(adapter: AsyncStoreAdapter = AsyncStoreAdapter.DOMAIN
};
}

/**
* Reset the store.
*
* */
export function reset() {
if (!isInitialized()) {
throw new Error('Async store not initialized.');
}

coreLog(`Resetting the store`);

getInstance(initializedAdapter).reset();
}

/**
* Delete a key from the store.
*
* @param {string} key
*/
export function del(key: string) {
if (!isInitialized()) {
throw new Error('Async store not initialized.');
}

coreLog(`Deleting ${key} from the store`);

getInstance(initializedAdapter).del(key);
}

/**
* Sets properties in the store.
*
Expand Down
76 changes: 76 additions & 0 deletions test/domain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as globalStore from '../src';
import Middleware from '../src/Middleware';
import { STORE_KEY } from '../src/StoreDomain';
import AsyncStoreAdapter from '../src/AsyncStoreAdapter';
import { getActiveDomain } from '../src/impl/domain';

describe('store: [adapter=DOMAIN]', () => {
const adapter = AsyncStoreAdapter.DOMAIN;
Expand Down Expand Up @@ -485,6 +486,81 @@ describe('store: [adapter=DOMAIN]', () => {
});
});

describe('Reset():', () => {
it('should reset the store by removing all values', (done) => {
const callback = () => {
globalStore.set({ foo: 'foo', bar: 'bar' });

expect(globalStore.isInitialized()).to.equal(true);

expect(globalStore.get('foo')).to.equal('foo');
expect(globalStore.get('bar')).to.equal('bar');

done();
};

globalStore.initialize(adapter)(callback);

expect(globalStore.reset).to.not.throw('Async store not initialized.');

globalStore.reset();

expect(globalStore.isInitialized()).to.equal(false);

const activeDomain = getActiveDomain();
expect(activeDomain[STORE_KEY]).to.equal(null);
});

it('should do nothing if the store is not initialized.', () => {
expect(globalStore.reset).to.throw('Async store not initialized.');
});


});

describe('del():', () => {
it('should delete a key from the store.', (done) => {
const callback = () => {
globalStore.set({ foo: 'foo', bar: 'bar' });

expect(globalStore.get('foo')).to.equal('foo');
expect(globalStore.get('bar')).to.equal('bar');

globalStore.del('foo');

expect(globalStore.get('foo')).to.equal(undefined);
expect(globalStore.get('bar')).to.equal('bar');

done();
};

globalStore.initialize(adapter)(callback);
});

it('should do nothing if the key does not exist.', (done) => {
const callback = () => {
globalStore.set({ foo: 'foo', bar: 'bar' });

expect(globalStore.get('foo')).to.equal('foo');
expect(globalStore.get('bar')).to.equal('bar');

expect(globalStore.del.bind(globalStore, 'baz')).to.not.throw('Async store not initialized.');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need this expect here as you have a separate test if the store is not initialized.

globalStore.del('baz');

expect(globalStore.get('foo')).to.equal('foo');
expect(globalStore.get('bar')).to.equal('bar');

done();
};

globalStore.initialize(adapter)(callback);
});

it('should do nothing if the store is not initialized.', () => {
expect(globalStore.del.bind(globalStore, 'foo')).to.throw('Async store not initialized.');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, why do we need to .bind here?

});
});

describe('Test Cases:', () => {
it('should work with a chain of sequentially invoked callbacks (synchronous).', (done) => {
const callback = () => {
Expand Down