Skip to content

Commit

Permalink
Merge pull request #189 from yezyilomo/fix-store.getStateValue
Browse files Browse the repository at this point in the history
Fix `store.getStateValue`
  • Loading branch information
yezyilomo authored Feb 2, 2024
2 parents 63c6546 + fcf6e41 commit 1cca3f5
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/Store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,9 +225,9 @@ export default class Store {
return storeItems;
}

getStateValue<ST, T = unknown>(key: string, config?): T | ST {
const state = this.getState<T>(key, config);
return state.getValue<ST>(config.selector);
getStateValue<ST, T = unknown>(key: string, selector?): T | ST {
const state = this.getState<T>(key);
return state.getValue<ST>(selector);
}

clear(fn?: () => void): void {
Expand Down
9 changes: 9 additions & 0 deletions tests/store.getState.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import StatePool, { State } from '../src/';


const store = StatePool.createStore({count: 0});

test('should get user values', () => {
const isStateInstance = store.getState("count") instanceof State;
expect(isStateInstance).toStrictEqual(true)
})
14 changes: 14 additions & 0 deletions tests/store.getStateValue.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import React from 'react';
import StatePool from '../src/';


const store = StatePool.createStore({
user: { name: "Yezy", age: 20 }
});

test('should get user values', () => {
const selector = (user): string => user.name;

expect(store.getStateValue("user")).toStrictEqual({ name: "Yezy", age: 20 })
expect(store.getStateValue("user", selector)).toStrictEqual("Yezy")
})
12 changes: 12 additions & 0 deletions tests/store.has.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import StatePool from '../src/';


const store = StatePool.createStore({ count: 0 });

test('should get user values', () => {
const inStore = store.has("count");
const notInStore = store.has("user");

expect(inStore).toStrictEqual(true);
expect(notInStore).toStrictEqual(false);
})
14 changes: 14 additions & 0 deletions tests/store.items.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import StatePool from '../src/';


const store = StatePool.createStore({
width: 10,
height: 15
});

test('should get user values', () => {
const [item1, item2] = store.items()

expect(item1).toStrictEqual(["width", 10, false]);
expect(item2).toStrictEqual(["height", 15, false]);
})
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ sidebar_position: 12
---

# store.getStateValue
This method is used to get state value directly from a store given its key, it's equivalent of calling `store.getState(key).getValue()`. You can pass `selector` too if you want to select part of the state.
This method is used to get state value directly from a store given its key, it's equivalent of calling `store.getState(key).getValue(selector?)`. You can pass `selector` too if you want to select part of the state.

```js
const userName = store.getStateValue("user", {selector: (user) => user.name});
const userName = store.getStateValue("user", (user) => user.name);
```

0 comments on commit 1cca3f5

Please sign in to comment.