-
Notifications
You must be signed in to change notification settings - Fork 427
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(preview): allow
null
as valid cache/memo value for preview fiel…
…ds (#7551) * refactor(preview): explicitly type the interface required by observeFields * test(preview): add a test for observePreview memo * fix(sanity): allow "null" as valid cache/memo value for preview fields
- Loading branch information
Showing
2 changed files
with
68 additions
and
6 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
packages/sanity/src/core/preview/__test__/observeFields.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import {describe, expect, it} from '@jest/globals' | ||
import {firstValueFrom, of, Subject} from 'rxjs' | ||
import {take, tap} from 'rxjs/operators' | ||
|
||
import {type ClientLike, createObserveFields} from '../observeFields' | ||
import {type InvalidationChannelEvent} from '../types' | ||
|
||
describe('observeFields', () => { | ||
it('should cache the last known value and emit sync', async () => { | ||
const client: ClientLike = { | ||
observable: { | ||
fetch: (query) => { | ||
expect(query).toEqual('[*[_id in ["foo"]][0...1]{_id,_rev,_type,bar}][0...1]') | ||
return of([ | ||
[ | ||
// no result | ||
], | ||
]) | ||
}, | ||
}, | ||
withConfig: () => client, | ||
} | ||
|
||
const invalidationChannel = new Subject<InvalidationChannelEvent>() | ||
const observeFields = createObserveFields({ | ||
invalidationChannel, | ||
client, | ||
}) | ||
const first = firstValueFrom(observeFields('foo', ['bar']).pipe(take(1))) | ||
invalidationChannel.next({type: 'connected'}) | ||
|
||
expect(await first).toMatchInlineSnapshot(`null`) | ||
|
||
// After we got first value from server and it turned out to be `null`, we should have `null` as the memoized sync value | ||
let syncValue = undefined | ||
observeFields('foo', ['bar']) | ||
.pipe( | ||
tap((value) => { | ||
syncValue = value | ||
}), | ||
take(1), | ||
) | ||
.subscribe() | ||
.unsubscribe() | ||
expect(syncValue).toBe(null) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters