diff --git a/packages/@sanity/migrate/src/runner/utils/__tests__/toSanityMutations.test.ts b/packages/@sanity/migrate/src/runner/utils/__tests__/toSanityMutations.test.ts new file mode 100644 index 000000000000..11f10778ae6e --- /dev/null +++ b/packages/@sanity/migrate/src/runner/utils/__tests__/toSanityMutations.test.ts @@ -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) + }) +}) diff --git a/packages/@sanity/migrate/src/runner/utils/toSanityMutations.ts b/packages/@sanity/migrate/src/runner/utils/toSanityMutations.ts index 9cd56b7d2375..6278daac5599 100644 --- a/packages/@sanity/migrate/src/runner/utils/toSanityMutations.ts +++ b/packages/@sanity/migrate/src/runner/utils/toSanityMutations.ts @@ -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[] } } }