Skip to content

Commit

Permalink
feat(core): add tests for when it's a live edit event
Browse files Browse the repository at this point in the history
  • Loading branch information
pedrobonamin committed Nov 29, 2024
1 parent 7d02649 commit b2d074e
Showing 1 changed file with 195 additions and 100 deletions.
295 changes: 195 additions & 100 deletions packages/sanity/src/core/store/events/getEditEvents.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {type EditDocumentVersionEvent} from 'sanity'
import {type EditDocumentVersionEvent, type UpdateLiveDocumentEvent} from 'sanity'
import {describe, expect, it} from 'vitest'

import {getEditEvents} from './getEditEvents'
Expand Down Expand Up @@ -50,120 +50,215 @@ describe('getEditEvents()', () => {
},
},
]
const expectedEvent: EditDocumentVersionEvent = {
type: 'EditDocumentVersion',
id: 'edit-tx-2',
documentId: 'versions.rkaihDvC1.f8dece19-c458-4cff-bf76-732b00617183',
timestamp: '2024-11-19T08:27:33.251404Z',
author: 'p8xDvUMxC',
contributors: ['p8xDvUMxC'],
releaseId: 'rkaihDvC1',
revisionId: 'edit-tx-2',
transactions: [
{
type: 'EditTransaction',
author: 'p8xDvUMxC',
timestamp: '2024-11-19T08:27:33.251404Z',
revisionId: 'edit-tx-2',
},
{
type: 'EditTransaction',
author: 'p8xDvUMxC',
timestamp: '2024-11-19T08:27:27.753746Z',
revisionId: 'edit-tx-1',
},
],
}

it('should merge the events if they are in the time window.', () => {
const events = getEditEvents(
editTransactions,
'versions.rkaihDvC1.f8dece19-c458-4cff-bf76-732b00617183',
false,
)
expect(events).toEqual([expectedEvent])
})
it("should not merge the events if they aren't in the time window.", () => {
const newTransaction = {
id: 'new-tx',
timestamp: '2024-11-19T08:35:27.753746Z',
author: 'p8xDvUMxC',
mutations: [],
documentIDs: ['versions.rkaihDvC1.f8dece19-c458-4cff-bf76-732b00617183'],
effects: {
'versions.rkaihDvC1.f8dece19-c458-4cff-bf76-732b00617183': {
apply: [11, 3, 23, 0, 9, 22, '9T08:27:2', 23, 18, 20, 15, 17, 'designer', 'role'],
revert: [11, 3, 23, 0, 9, 22, '8T17:48:0', 23, 18, 20, 15, 17, 'developer', 'role'],
},
},
}
const newEvent: EditDocumentVersionEvent = {
describe('when the document is not liveEdit', () => {
const expectedEvent: EditDocumentVersionEvent = {
type: 'EditDocumentVersion',
id: 'edit-tx-2',
documentId: 'versions.rkaihDvC1.f8dece19-c458-4cff-bf76-732b00617183',
timestamp: '2024-11-19T08:35:27.753746Z',
id: 'new-tx',
timestamp: '2024-11-19T08:27:33.251404Z',
author: 'p8xDvUMxC',
contributors: ['p8xDvUMxC'],
releaseId: 'rkaihDvC1',
revisionId: 'new-tx',
revisionId: 'edit-tx-2',
transactions: [
{
type: 'EditTransaction',
author: 'p8xDvUMxC',
timestamp: '2024-11-19T08:35:27.753746Z',
revisionId: 'new-tx',
timestamp: '2024-11-19T08:27:33.251404Z',
revisionId: 'edit-tx-2',
},
{
type: 'EditTransaction',
author: 'p8xDvUMxC',
timestamp: '2024-11-19T08:27:27.753746Z',
revisionId: 'edit-tx-1',
},
],
}
const events = getEditEvents(
[...editTransactions, newTransaction],
'versions.rkaihDvC1.f8dece19-c458-4cff-bf76-732b00617183',
false,
)
expect(events).toEqual([newEvent, expectedEvent])
})
it('should filter non edit events', () => {
const creationTransaction = {
id: 'create-tx',
timestamp: '2024-11-19T08:35:27.753746Z',
author: 'p8xDvUMxC',
mutations: [],
documentIDs: ['versions.rkaihDvC1.f8dece19-c458-4cff-bf76-732b00617183'],
effects: {
'versions.rkaihDvC1.f8dece19-c458-4cff-bf76-732b00617183': {
apply: [11, 3, 23, 0, 9, 22, '9T08:27:2', 23, 18, 20, 15, 17, 'designer', 'role'],
revert: [0, null],

it('should merge the events if they are in the time window.', () => {
const events = getEditEvents(
editTransactions,
'versions.rkaihDvC1.f8dece19-c458-4cff-bf76-732b00617183',
false,
)
expect(events).toEqual([expectedEvent])
})
it("should not merge the events if they aren't in the time window.", () => {
const newTransaction = {
id: 'new-tx',
timestamp: '2024-11-19T08:35:27.753746Z',
author: 'p8xDvUMxC',
mutations: [],
documentIDs: ['versions.rkaihDvC1.f8dece19-c458-4cff-bf76-732b00617183'],
effects: {
'versions.rkaihDvC1.f8dece19-c458-4cff-bf76-732b00617183': {
apply: [11, 3, 23, 0, 9, 22, '9T08:27:2', 23, 18, 20, 15, 17, 'designer', 'role'],
revert: [11, 3, 23, 0, 9, 22, '8T17:48:0', 23, 18, 20, 15, 17, 'developer', 'role'],
},
},
},
}
const deleteTx = {
id: 'delete-tx',
timestamp: '2024-11-19T08:35:27.753746Z',
author: 'p8xDvUMxC',
mutations: [],
documentIDs: ['versions.rkaihDvC1.f8dece19-c458-4cff-bf76-732b00617183'],
effects: {
'versions.rkaihDvC1.f8dece19-c458-4cff-bf76-732b00617183': {
apply: [0, null],
revert: [11, 3, 23, 0, 9, 22, '9T08:27:2', 23, 18, 20, 15, 17, 'designer', 'role'],
}
const newEvent: EditDocumentVersionEvent = {
type: 'EditDocumentVersion',
documentId: 'versions.rkaihDvC1.f8dece19-c458-4cff-bf76-732b00617183',
timestamp: '2024-11-19T08:35:27.753746Z',
id: 'new-tx',
author: 'p8xDvUMxC',
contributors: ['p8xDvUMxC'],
releaseId: 'rkaihDvC1',
revisionId: 'new-tx',
transactions: [
{
type: 'EditTransaction',
author: 'p8xDvUMxC',
timestamp: '2024-11-19T08:35:27.753746Z',
revisionId: 'new-tx',
},
],
}
const events = getEditEvents(
[...editTransactions, newTransaction],
'versions.rkaihDvC1.f8dece19-c458-4cff-bf76-732b00617183',
false,
)
expect(events).toEqual([newEvent, expectedEvent])
})
it('should filter non edit events', () => {
const creationTransaction = {
id: 'create-tx',
timestamp: '2024-11-19T08:35:27.753746Z',
author: 'p8xDvUMxC',
mutations: [],
documentIDs: ['versions.rkaihDvC1.f8dece19-c458-4cff-bf76-732b00617183'],
effects: {
'versions.rkaihDvC1.f8dece19-c458-4cff-bf76-732b00617183': {
apply: [11, 3, 23, 0, 9, 22, '9T08:27:2', 23, 18, 20, 15, 17, 'designer', 'role'],
revert: [0, null],
},
},
},
}
const undefinedTx = {
id: 'undefined-tx',
timestamp: '2024-11-19T08:35:27.753746Z',
}
const deleteTx = {
id: 'delete-tx',
timestamp: '2024-11-19T08:35:27.753746Z',
author: 'p8xDvUMxC',
mutations: [],
documentIDs: ['versions.rkaihDvC1.f8dece19-c458-4cff-bf76-732b00617183'],
effects: {
'versions.rkaihDvC1.f8dece19-c458-4cff-bf76-732b00617183': {
apply: [0, null],
revert: [11, 3, 23, 0, 9, 22, '9T08:27:2', 23, 18, 20, 15, 17, 'designer', 'role'],
},
},
}
const undefinedTx = {
id: 'undefined-tx',
timestamp: '2024-11-19T08:35:27.753746Z',
author: 'p8xDvUMxC',
mutations: [],
documentIDs: ['versions.rkaihDvC1.f8dece19-c458-4cff-bf76-732b00617183'],
effects: {
'versions.rkaihDvC1.f8dece19-c458-4cff-bf76-732b00617183': undefined,
},
}
const events = getEditEvents(
[deleteTx, ...editTransactions, undefinedTx, creationTransaction],
'versions.rkaihDvC1.f8dece19-c458-4cff-bf76-732b00617183',
false,
)
expect(events).toEqual([expectedEvent])
})
})
describe('when the document is liveEdit', () => {
const expectedEvent: UpdateLiveDocumentEvent = {
type: 'UpdateLiveDocument',
id: 'edit-tx-2',
documentId: 'versions.rkaihDvC1.f8dece19-c458-4cff-bf76-732b00617183',
timestamp: '2024-11-19T08:27:33.251404Z',
author: 'p8xDvUMxC',
mutations: [],
documentIDs: ['versions.rkaihDvC1.f8dece19-c458-4cff-bf76-732b00617183'],
effects: {
'versions.rkaihDvC1.f8dece19-c458-4cff-bf76-732b00617183': undefined,
},
revisionId: 'edit-tx-2',
}
const events = getEditEvents(
[deleteTx, ...editTransactions, undefinedTx, creationTransaction],
'versions.rkaihDvC1.f8dece19-c458-4cff-bf76-732b00617183',
false,
)
expect(events).toEqual([expectedEvent])
it('should merge the events if they are in the time window.', () => {
const events = getEditEvents(
editTransactions,
'versions.rkaihDvC1.f8dece19-c458-4cff-bf76-732b00617183',
true,
)
expect(events).toEqual([expectedEvent])
})
it("should not merge the events if they aren't in the time window.", () => {
const newTransaction = {
id: 'new-tx',
timestamp: '2024-11-19T08:35:27.753746Z',
author: 'p8xDvUMxC',
mutations: [],
documentIDs: ['versions.rkaihDvC1.f8dece19-c458-4cff-bf76-732b00617183'],
effects: {
'versions.rkaihDvC1.f8dece19-c458-4cff-bf76-732b00617183': {
apply: [11, 3, 23, 0, 9, 22, '9T08:27:2', 23, 18, 20, 15, 17, 'designer', 'role'],
revert: [11, 3, 23, 0, 9, 22, '8T17:48:0', 23, 18, 20, 15, 17, 'developer', 'role'],
},
},
}
const newEvent: UpdateLiveDocumentEvent = {
type: 'UpdateLiveDocument',
documentId: 'versions.rkaihDvC1.f8dece19-c458-4cff-bf76-732b00617183',
timestamp: '2024-11-19T08:35:27.753746Z',
id: 'new-tx',
author: 'p8xDvUMxC',
revisionId: 'new-tx',
}
const events = getEditEvents(
[...editTransactions, newTransaction],
'versions.rkaihDvC1.f8dece19-c458-4cff-bf76-732b00617183',
true,
)
expect(events).toEqual([newEvent, expectedEvent])
})
it('should filter non edit events', () => {
const creationTransaction = {
id: 'create-tx',
timestamp: '2024-11-19T08:35:27.753746Z',
author: 'p8xDvUMxC',
mutations: [],
documentIDs: ['versions.rkaihDvC1.f8dece19-c458-4cff-bf76-732b00617183'],
effects: {
'versions.rkaihDvC1.f8dece19-c458-4cff-bf76-732b00617183': {
apply: [11, 3, 23, 0, 9, 22, '9T08:27:2', 23, 18, 20, 15, 17, 'designer', 'role'],
revert: [0, null],
},
},
}
const deleteTx = {
id: 'delete-tx',
timestamp: '2024-11-19T08:35:27.753746Z',
author: 'p8xDvUMxC',
mutations: [],
documentIDs: ['versions.rkaihDvC1.f8dece19-c458-4cff-bf76-732b00617183'],
effects: {
'versions.rkaihDvC1.f8dece19-c458-4cff-bf76-732b00617183': {
apply: [0, null],
revert: [11, 3, 23, 0, 9, 22, '9T08:27:2', 23, 18, 20, 15, 17, 'designer', 'role'],
},
},
}
const undefinedTx = {
id: 'undefined-tx',
timestamp: '2024-11-19T08:35:27.753746Z',
author: 'p8xDvUMxC',
mutations: [],
documentIDs: ['versions.rkaihDvC1.f8dece19-c458-4cff-bf76-732b00617183'],
effects: {
'versions.rkaihDvC1.f8dece19-c458-4cff-bf76-732b00617183': undefined,
},
}
const events = getEditEvents(
[deleteTx, ...editTransactions, undefinedTx, creationTransaction],
'versions.rkaihDvC1.f8dece19-c458-4cff-bf76-732b00617183',
true,
)
expect(events).toEqual([expectedEvent])
})
})
})

// TODO: Repeat tests for the live edit events

0 comments on commit b2d074e

Please sign in to comment.