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

feat: parity between overlayDrafts and perspectives=previewDrafts #92

Merged
merged 2 commits into from
Jun 27, 2023
Merged
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
9 changes: 6 additions & 3 deletions src/syncingDataset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import {getPublishedId} from './drafts'
import {applyPatchWithoutRev} from './patch'
import {Config, EnvImplementations, MutationEvent, Subscription} from './types'
import {compareString} from './utils'

const DEBOUNCE_MS = 25

Expand Down Expand Up @@ -37,7 +38,9 @@
stagedDocs = undefined
flushTimeout = undefined
previousTrx = undefined
onNotifyUpdate(overlayDrafts ? overlay(docs) : docs)
const finalDocs = overlayDrafts ? overlay(docs) : docs
finalDocs.sort((a, b) => compareString(a._id, b._id))
onNotifyUpdate(finalDocs)
}

if (!useListener) {
Expand Down Expand Up @@ -150,7 +153,7 @@
groups.forEach((group, id) => {
const document = documents.find((doc) => doc._id === id)
if (!document) {
// @todo handle

Check warning on line 156 in src/syncingDataset.ts

View workflow job for this annotation

GitHub Actions / Lint & Build

Unexpected 'todo' comment: '@todo handle'
// eslint-disable-next-line no-console
console.warn('Received mutation for missing document %s', id)
return
Expand Down Expand Up @@ -188,7 +191,7 @@
overlayed.set(getPublishedId(doc), pretendThatItsPublished(doc))
} else if (!existing) {
// Published documents only override if draft doesn't exist
overlayed.set(doc._id, doc)
overlayed.set(doc._id, {...doc, _originalId: doc._id})
}
})

Expand All @@ -198,5 +201,5 @@
// Strictly speaking it would be better to allow groq-js to resolve `draft.<id>`,
// but for now this will have to do
function pretendThatItsPublished(doc: SanityDocument): SanityDocument {
return {...doc, _id: getPublishedId(doc)}
return {...doc, _id: getPublishedId(doc), _originalId: doc._id}
}
5 changes: 5 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export function compareString(a: string, b: string): number {
if (a > b) return 1
if (a < b) return -1
return 0
}
56 changes: 50 additions & 6 deletions test/query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {GroqStore} from '../src/types'
import {describe, beforeAll, afterAll, test, expect} from 'vitest'

describe(
'query',
'query with overlayDrafts',
() => {
let store: GroqStore

Expand Down Expand Up @@ -32,13 +32,57 @@ describe(
expect(await store.query(groq`*[_type == "vendor"][].title | order(@ asc) [3]`)).toEqual(
'Freia'
)
expect(await store.query(groq`array::unique(*._type)`)).toEqual([
'category',
'product',
'sanity.imageAsset',
'vendor',
expect(new Set(await store.query(groq`array::unique(*._type)`))).toEqual(
new Set(['category', 'product', 'sanity.imageAsset', 'vendor'])
)
})

test('populates _originalId', async () => {
const id = await store.query(groq`*[_type == "vendor"][0]._originalId`)
expect(id).toBe('01ca40b6-e7fd-4676-af25-33f591de51c0')
})

test('sorts based on _id', async () => {
const titles = await store.query(groq`*[_type == "vendor"].title`)
expect(titles).toEqual([
'Kracie',
'Nestlè',
'Ferrero',
'Freia',
'Malaco',
'Chocolates Garoto',
'Katjes',
'Cadbury',
'Totte Gott',
])
})
},
{timeout: 30000}
)

describe('query without overlayDrafts', () => {
let store: GroqStore

beforeAll(() => {
store = groqStore({...config, listen: false, overlayDrafts: false})
})

afterAll(async () => {
await store.close()
})

test('sorts based on _id', async () => {
const titles = await store.query(groq`*[_type == "vendor"].title`)
expect(titles).toEqual([
'Kracie',
'Nestlè',
'Ferrero',
'Freia',
'Malaco',
'Chocolates Garoto',
'Katjes',
'Cadbury',
'Totte Gott',
])
})
})
Loading