-
Notifications
You must be signed in to change notification settings - Fork 448
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(migration): fixes issues where patch would happen twice (#5638)
* fix(migration): fixes issues where patch would happen twice * fix(migrate): fix bug with duplicated patches --------- Co-authored-by: Bjørge Næss <bjoerge@gmail.com>
- Loading branch information
Showing
2 changed files
with
116 additions
and
1 deletion.
There are no files selected for viewing
114 changes: 114 additions & 0 deletions
114
packages/@sanity/migrate/src/runner/utils/__tests__/toSanityMutations.test.ts
This file contains 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,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) | ||
}) | ||
}) |
This file contains 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