diff --git a/packages/@sanity/mutator/src/document/Mutation.ts b/packages/@sanity/mutator/src/document/Mutation.ts index 61a23a4721b..ad145532652 100644 --- a/packages/@sanity/mutator/src/document/Mutation.ts +++ b/packages/@sanity/mutator/src/document/Mutation.ts @@ -1,5 +1,3 @@ -import {isString} from 'lodash' - import {Patcher} from '../patch' import {debug} from './debug' import {luid} from './luid' @@ -110,9 +108,7 @@ export class Mutation { // creation requires a _createdAt const getGuaranteedCreatedAt = (doc: Doc): string => - doc && isString(doc._createdAt) - ? doc._createdAt - : this.params.timestamp || new Date().toISOString() + doc?._createdAt || this.params.timestamp || new Date().toISOString() this.mutations.forEach((mutation) => { if (mutation.create) { diff --git a/packages/@sanity/mutator/test/SquashingBuffer.test.ts b/packages/@sanity/mutator/test/SquashingBuffer.test.ts index 4ae821d92a3..6544bddf769 100644 --- a/packages/@sanity/mutator/test/SquashingBuffer.test.ts +++ b/packages/@sanity/mutator/test/SquashingBuffer.test.ts @@ -109,6 +109,35 @@ test('de-duplicate createIfNotExists', () => { expect(tx2 && tx2.mutations.length).toBe(1) }) +test.each(['create', 'createIfNotExists', 'createOrReplace'])( + '%s defaults to current created at time', + (createFnc) => { + const globalMockDate = new Date('2020-01-01T12:34:55.000Z') + const globalDateSpy = jest.spyOn(global, 'Date').mockReturnValue(globalMockDate) + + const sb = new SquashingBuffer(null) + + add(sb, {[createFnc]: {_id: '1', _type: 'test', a: 'A string value'}}) + + const tx = sb.purge('txn_id') + if (!tx) { + throw new Error('buffer purge did not result in a mutation') + } + + const final = tx.apply(null) + + expect(final).toEqual({ + _id: '1', + _rev: 'txn_id', + _createdAt: '2020-01-01T12:34:55.000Z', + _type: 'test', + a: 'A string value', + }) + + globalDateSpy.mockRestore() + }, +) + test('de-duplicate create respects deletes', () => { const globalMockDate = new Date('2020-01-01T12:34:55.000Z') const globalDateSpy = jest.spyOn(global, 'Date').mockReturnValue(globalMockDate)