chore(sync): Add message-dispatcher unit tests

This commit is contained in:
Alexis Métaireau 2024-05-14 11:34:00 +02:00
parent 74a1670c9d
commit 92676e03ae
3 changed files with 47 additions and 7 deletions

View file

@ -47,10 +47,11 @@ export class MessagesDispatcher {
} }
dispatch({ kind, ...payload }) { dispatch({ kind, ...payload }) {
console.log('received message', kind, payload)
if (kind == 'operation') { if (kind == 'operation') {
let updater = this.getUpdater(payload.subject, payload.metadata) let updater = this.getUpdater(payload.subject, payload.metadata)
updater.applyMessage(payload) updater.applyMessage(payload)
} else {
throw new Error(`Unknown dispatch kind: ${kind}`)
} }
} }
} }

View file

@ -20,7 +20,7 @@ class BaseUpdater {
if (part in currentObj) return currentObj[part] if (part in currentObj) return currentObj[part]
}, obj) }, obj)
// In case the given path doesn't exist, bail out // In case the given path doesn't exist, stop here
if (objectToSet === undefined) return if (objectToSet === undefined) return
// Set the value (or delete it) // Set the value (or delete it)

View file

@ -1,13 +1,52 @@
import { describe, it } from 'mocha' import { describe, it } from 'mocha'
import sinon from 'sinon'
import pkg from 'chai' import pkg from 'chai'
const { expect } = pkg const { expect } = pkg
import { import { MapUpdater } from '../js/modules/sync/updaters.js'
MapUpdater, import { MessagesDispatcher, SyncEngine } from '../js/modules/sync/engine.js'
DataLayerUpdater,
FeatureUpdater, describe('SyncEngine', () => {
} from '../js/modules/sync/updaters.js' it('should initialize methods even before start', function () {
const engine = new SyncEngine({})
engine.upsert()
engine.update()
engine.delete()
})
})
describe('MessageDispatcher', () => {
describe('#dispatch', function () {
it('should raise an error on unknown updater', function () {
const dispatcher = new MessagesDispatcher({})
expect(() => {
dispatcher.dispatch({
kind: 'operation',
subject: 'unknown',
metadata: {},
})
}).to.throw(Error)
})
it('should produce an error on malformated messages', function () {
const dispatcher = new MessagesDispatcher({})
expect(() => {
dispatcher.dispatch({
yeah: 'yeah',
payload: { foo: 'bar' },
})
}).to.throw(Error)
})
it('should raise an unknown operations', function () {
const dispatcher = new MessagesDispatcher({})
expect(() => {
dispatcher.dispatch({
kind: 'something-else',
})
}).to.throw(Error)
})
})
})
describe('Updaters', () => { describe('Updaters', () => {
describe('BaseUpdater', function () { describe('BaseUpdater', function () {