Skip to content

Commit

Permalink
fix(migration): fixes issues where patch would happen twice (#5638)
Browse files Browse the repository at this point in the history
* fix(migration): fixes issues where patch would happen twice

* fix(migrate): fix bug with duplicated patches

---------

Co-authored-by: Bjørge Næss <[email protected]>
  • Loading branch information
binoy14 and bjoerge authored Feb 1, 2024
1 parent 2ba3b9f commit 958869d
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
import arrify from 'arrify'
import {SanityEncoder} from '@bjoerge/mutiny'
import {toSanityMutations, TransactionPayload} from '../toSanityMutations'
import {Mutation, Transaction} from '../../../mutations'

jest.mock('@bjoerge/mutiny', () => {
const actual = jest.requireActual('@bjoerge/mutiny')
return {
...actual,
SanityEncoder: {
encode: jest.fn().mockImplementation(actual.SanityEncoder.encode),
},
}
})

afterEach(() => {
jest.clearAllMocks()
})

describe('#toSanityMutations', () => {
it('should handle single mutation', async () => {
const mockMutation: Mutation = {
type: 'patch',
id: 'drafts.f9b1dc7a-9dd6-4949-8292-9738bf9e2969',
patches: [{path: ['prependTest'], op: {type: 'setIfMissing', value: []}}],
}

const mockMutationIterable = async function* () {
yield mockMutation
}

const iterable = toSanityMutations(mockMutationIterable())

const result = []
for await (const mutation of iterable) {
result.push(mutation)
}

expect(result.flat()).toEqual(SanityEncoder.encode([mockMutation] as any))
expect(SanityEncoder.encode).toHaveBeenCalledWith([mockMutation])
})

it('should handle multiple mutations', async () => {
const mockMutations: Mutation[] = [
{
type: 'patch',
id: 'drafts.f9b1dc7a-9dd6-4949-8292-9738bf9e2969',
patches: [{path: ['prependTest'], op: {type: 'setIfMissing', value: []}}],
},
{
type: 'patch',
id: 'drafts.f9b1dc7a-9dd6-4949-8292-9738bf9e2969',
patches: [
{
path: ['prependTest'],
op: {
type: 'insert',
referenceItem: 0,
position: 'before',
items: [{_type: 'oops', name: 'test'}],
},
},
],
},
]

const mockMutationIterable = async function* () {
yield mockMutations
}

const iterable = toSanityMutations(mockMutationIterable())

const result = []
for await (const mutation of iterable) {
result.push(mutation)
}

expect(result.flat()).toEqual(SanityEncoder.encode(mockMutations as any))
expect(SanityEncoder.encode).toHaveBeenCalledWith(mockMutations)
})

it('should handle transaction', async () => {
const mockTransaction: Transaction = {
type: 'transaction',
id: 'transaction1',
mutations: [
{
type: 'patch',
id: 'drafts.f9b1dc7a-9dd6-4949-8292-9738bf9e2969',
patches: [{path: ['prependTest'], op: {type: 'setIfMissing', value: []}}],
},
],
}

const iterable = toSanityMutations(
(async function* () {
yield mockTransaction
})(),
)

const result = []
for await (const mutation of iterable) {
result.push(mutation)
}

const expected: TransactionPayload = {
transactionId: mockTransaction.id,
mutations: SanityEncoder.encode(mockTransaction.mutations as any),
}

expect(result).toEqual([expected])
expect(SanityEncoder.encode).toHaveBeenCalledWith(mockTransaction.mutations)
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export async function* toSanityMutations(
}
continue
}
yield SanityEncoder.encode(mutation as any[]) as SanityMutation[]

yield SanityEncoder.encode(arrify(mut) as any[]) as SanityMutation[]
}
}
}

0 comments on commit 958869d

Please sign in to comment.