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

Add reset and del functionality #144

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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 @@
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.');

Check warning on line 106 in src/impl/domain.ts

View check run for this annotation

Codecov / codecov/patch

src/impl/domain.ts#L106

Added line #L106 was not covered by tests

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

Check warning on line 109 in src/impl/domain.ts

View check run for this annotation

Codecov / codecov/patch

src/impl/domain.ts#L108-L109

Added lines #L108 - L109 were not covered by tests
}

/**
* 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;

Check warning on line 123 in src/impl/domain.ts

View check run for this annotation

Codecov / codecov/patch

src/impl/domain.ts#L123

Added line #L123 was not covered by tests
}

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 @@
};
}

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

coreLog(`Resetting the store`);

Check warning on line 127 in src/index.ts

View check run for this annotation

Codecov / codecov/patch

src/index.ts#L127

Added line #L127 was not covered by tests

getInstance(initializedAdapter).reset();

Check warning on line 129 in src/index.ts

View check run for this annotation

Codecov / codecov/patch

src/index.ts#L129

Added line #L129 was not covered by tests
}

/**
* 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
Loading