Skip to content

Commit

Permalink
docs(changeset): Handle null values in Sourced fields
Browse files Browse the repository at this point in the history
  • Loading branch information
igorkamyshev committed Nov 17, 2022
1 parent 3419015 commit 072aae1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/few-bags-report.md
@@ -0,0 +1,5 @@
---
'@farfetched/core': patch
---

Handle null values in Sourced fields
24 changes: 24 additions & 0 deletions packages/core/src/misc/__tests__/sourced.spec.ts
Expand Up @@ -32,6 +32,18 @@ describe('normalizeSourced (with clock)', () => {
expect(scope.getState($normalized)).toBe(false);
});

test('handle null value', async () => {
const clock = createEvent();

const $normalized = normalizeSourced({ field: null, clock });

const scope = fork();

await allSettled(clock, { scope });

expect(scope.getState($normalized)).toBe(null);
});

test('handle store value', async () => {
const clock = createEvent();

Expand Down Expand Up @@ -132,6 +144,18 @@ describe('normalizeSourced (with source)', () => {
expect(scope.getState($normalized)).toBe(false);
});

test('handle null value', async () => {
const source = createStore(null);

const $normalized = normalizeSourced({ field: null, source });

const scope = fork();

await allSettled(source, { scope, params: 12 });

expect(scope.getState($normalized)).toBe(null);
});

test('handle store value', async () => {
const $source = createStore('');

Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/misc/sourced.ts
Expand Up @@ -43,7 +43,7 @@ function normalizeSourced<Data, Result, Source>({
const $storeField = field as Store<Result>;

sample({ clock, source: $storeField, target: $target });
} else if (field.source && field.fn) {
} else if (field?.source && field?.fn) {
const callbackField = field as CallbackWithSource<Data, Result, Source>;

sample({
Expand Down Expand Up @@ -71,7 +71,7 @@ function normalizeSourced<Data, Result, Source>({
const $storeField = field as Store<Result>;

$target = $storeField;
} else if (field.source && field.fn) {
} else if (field?.source && field?.fn) {
const callbackField = field as CallbackWithSource<Data, Result, Source>;

$target = combine($source, callbackField.source, callbackField.fn);
Expand Down

0 comments on commit 072aae1

Please sign in to comment.