-
Notifications
You must be signed in to change notification settings - Fork 3
Add SyncedDocumentOptions #36
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,202 @@ | ||
| import { expect } from 'chai' | ||
| import { asNumber, asObject, asOptional, asString } from 'cleaners' | ||
| import { describe, it } from 'mocha' | ||
| import { DocumentScope } from 'nano' | ||
|
|
||
| import { syncedDocument } from '../../src/couchdb/synced-document' | ||
|
|
||
| const asConfig = asObject({ | ||
| name: asOptional(asString, 'default'), | ||
| count: asOptional(asNumber, 0) | ||
| }) | ||
|
|
||
| type Config = ReturnType<typeof asConfig> | ||
|
|
||
| function makeMockDb( | ||
| store: Record<string, { _rev: string; [key: string]: unknown }> = {} | ||
| ): DocumentScope<unknown> { | ||
| let revCounter = 0 | ||
| return { | ||
| get: async (id: string) => { | ||
| const doc = store[id] | ||
| if (doc == null) | ||
| throw Object.assign(new Error('not_found'), { error: 'not_found' }) | ||
| return { _id: id, ...doc } | ||
| }, | ||
| insert: async (doc: { | ||
| _id: string | ||
| _rev?: string | ||
| [key: string]: unknown | ||
| }) => { | ||
| const rev = `${++revCounter}-abc` | ||
| const { _id, ...rest } = doc | ||
| store[_id] = { ...rest, _rev: rev } | ||
| return { ok: true, id: _id, rev } | ||
| } | ||
| } as any | ||
| } | ||
|
|
||
| describe('syncedDocument', function () { | ||
| it('initializes with fallback from cleaner({})', function () { | ||
| const doc = syncedDocument('config', asConfig) | ||
| expect(doc.doc).deep.equals({ name: 'default', count: 0 }) | ||
| expect(doc.rev).equals(undefined) | ||
| expect(doc.id).equals('config') | ||
| }) | ||
|
|
||
| it('creates missing documents on sync', async function () { | ||
| const doc = syncedDocument('config', asConfig) | ||
| const db = makeMockDb() | ||
|
|
||
| await doc.sync(db) | ||
|
|
||
| expect(doc.doc).deep.equals({ name: 'default', count: 0 }) | ||
| expect(doc.rev).to.be.a('string') | ||
| }) | ||
|
|
||
| it('reads existing clean documents', async function () { | ||
| const store = { | ||
| config: { _rev: '1-existing', name: 'prod', count: 42 } | ||
| } | ||
| const doc = syncedDocument('config', asConfig) | ||
| const db = makeMockDb(store) | ||
|
|
||
| await doc.sync(db) | ||
|
|
||
| expect(doc.doc).deep.equals({ name: 'prod', count: 42 }) | ||
| expect(doc.rev).equals('1-existing') | ||
| }) | ||
|
|
||
| it('repairs dirty documents (extra fields)', async function () { | ||
| const store = { | ||
| config: { _rev: '1-existing', name: 'prod', count: 42, extra: 'junk' } | ||
| } | ||
| const doc = syncedDocument('config', asConfig) | ||
| const db = makeMockDb(store) | ||
|
|
||
| await doc.sync(db) | ||
|
|
||
| expect(doc.doc).deep.equals({ name: 'prod', count: 42 }) | ||
| expect(doc.rev).not.equals('1-existing') | ||
| }) | ||
|
|
||
| it('fires onChange when document is created', async function () { | ||
| const doc = syncedDocument('config', asConfig) | ||
| const db = makeMockDb() | ||
| const changes: Config[] = [] | ||
| doc.onChange(value => changes.push(value)) | ||
|
|
||
| await doc.sync(db) | ||
|
|
||
| expect(changes).to.have.length(1) | ||
| expect(changes[0]).deep.equals({ name: 'default', count: 0 }) | ||
| }) | ||
|
|
||
| it('does not fire onChange when nothing changed', async function () { | ||
| const store = { | ||
| config: { _rev: '1-existing', name: 'default', count: 0 } | ||
| } | ||
| const doc = syncedDocument('config', asConfig) | ||
| const db = makeMockDb(store) | ||
| await doc.sync(db) | ||
|
|
||
| const changes: Config[] = [] | ||
| doc.onChange(value => changes.push(value)) | ||
| await doc.sync(db) | ||
|
|
||
| expect(changes).to.have.length(0) | ||
| }) | ||
|
|
||
| describe('cleanFailStrategy = "reset" (default)', function () { | ||
| it('resets to fallback when document fails cleaner', async function () { | ||
| const store = { | ||
| config: { _rev: '1-existing', name: 'prod', count: 42 } | ||
| } | ||
| const doc = syncedDocument('config', asConfig) | ||
| const db = makeMockDb(store) | ||
|
|
||
| await doc.sync(db) | ||
| expect(doc.doc).deep.equals({ name: 'prod', count: 42 }) | ||
|
|
||
| // Corrupt the document: | ||
| store.config = { | ||
| _rev: store.config._rev, | ||
| name: 999 as any, | ||
| count: 'bad' as any | ||
| } | ||
|
|
||
| await doc.sync(db) | ||
| expect(doc.doc).deep.equals({ name: 'default', count: 0 }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('cleanFailStrategy = "preserve"', function () { | ||
| it('preserves last good value when document fails cleaner', async function () { | ||
| const store = { | ||
| config: { _rev: '1-existing', name: 'prod', count: 42 } | ||
| } | ||
| const doc = syncedDocument('config', asConfig, { | ||
| cleanFailStrategy: 'preserve' | ||
| }) | ||
| const db = makeMockDb(store) | ||
|
|
||
| await doc.sync(db) | ||
| expect(doc.doc).deep.equals({ name: 'prod', count: 42 }) | ||
|
|
||
| store.config = { | ||
| _rev: store.config._rev, | ||
| name: 999 as any, | ||
| count: 'bad' as any | ||
| } | ||
|
|
||
| await doc.sync(db) | ||
| expect(doc.doc).deep.equals({ name: 'prod', count: 42 }) | ||
| }) | ||
|
|
||
| it('falls back to cleaner({}) when no previous good state exists', async function () { | ||
| const store = { | ||
| config: { _rev: '1-existing', name: 999 as any, count: 'bad' as any } | ||
| } | ||
| const doc = syncedDocument('config', asConfig, { | ||
| cleanFailStrategy: 'preserve' | ||
| }) | ||
| const db = makeMockDb(store) | ||
|
|
||
| await doc.sync(db) | ||
| expect(doc.doc).deep.equals({ name: 'default', count: 0 }) | ||
| }) | ||
| }) | ||
|
|
||
| describe('onCleanFail callback', function () { | ||
| it('fires with the error when the cleaner fails', async function () { | ||
| const store = { | ||
| config: { _rev: '1-existing', name: 999 as any, count: 'bad' as any } | ||
| } | ||
| const errors: unknown[] = [] | ||
| const doc = syncedDocument('config', asConfig, { | ||
| onCleanFail: error => errors.push(error) | ||
| }) | ||
| const db = makeMockDb(store) | ||
|
|
||
| await doc.sync(db) | ||
|
|
||
| expect(errors).to.have.length(1) | ||
| expect(errors[0]).to.be.an('error') | ||
| }) | ||
|
|
||
| it('does not fire when the cleaner succeeds', async function () { | ||
| const store = { | ||
| config: { _rev: '1-existing', name: 'prod', count: 42 } | ||
| } | ||
| const errors: unknown[] = [] | ||
| const doc = syncedDocument('config', asConfig, { | ||
| onCleanFail: error => errors.push(error) | ||
| }) | ||
| const db = makeMockDb(store) | ||
|
|
||
| await doc.sync(db) | ||
|
|
||
| expect(errors).to.have.length(0) | ||
| }) | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can the return value be
DocumentScope<unknown>, and then put thereturn {} as anyhack inside? That would make it clearer what this is supposed to mock.