Skip to content

Commit

Permalink
test(migrate): update types for test to better match usage (#5781)
Browse files Browse the repository at this point in the history
  • Loading branch information
binoy14 authored Feb 19, 2024
1 parent 84a6015 commit 15c3bc1
Showing 1 changed file with 24 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,26 +1,32 @@
import {describe, expect, it, jest} from '@jest/globals'
import {type SanityDocument} from '@sanity/types'

import {createIfNotExists} from '../../mutations'
import {type Migration, type NodeMigration} from '../../types'
import {
type DocumentMigrationReturnValue,
type Migration,
type MigrationContext,
type NodeMigration,
} from '../../types'
import {
createAsyncIterableMutation,
normalizeMigrateDefinition,
} from '../normalizeMigrateDefinition'

const mockAsyncIterableIterator = () => {
const data = [{_id: 'mockId', _type: 'mockDocumentType'}]
let index = 0

return {
next: jest.fn().mockImplementation(() => {
if (index < data.length) {
return Promise.resolve({value: data[index++], done: false})
}
return Promise.resolve({value: undefined, done: true})
}),
[Symbol.asyncIterator]: jest.fn().mockImplementation(function (this: unknown) {
return this
}),
const data: SanityDocument[] = [
{
_id: 'mockId',
_type: 'mockDocumentType',
_updatedAt: '2024-02-16T14:13:59Z',
_rev: 'xyz',
_createdAt: '2024-02-16T14:13:59Z',
},
]
return async function* documents() {
for (let index = 0; index < data.length; index++) {
yield data[index]
}
}
}

Expand Down Expand Up @@ -58,7 +64,7 @@ describe('#normalizeMigrateDefinition', () => {
const result = normalizeMigrateDefinition(mockMigration)
const res = []

for await (const item of result(mockAsyncIterableIterator as any, {} as any)) {
for await (const item of result(mockAsyncIterableIterator(), {} as any)) {
res.push(item)
}

Expand All @@ -79,7 +85,7 @@ describe('#normalizeMigrateDefinition', () => {
const result = normalizeMigrateDefinition(mockMigration)
const res = []

for await (const item of result(mockAsyncIterableIterator as any, {} as any)) {
for await (const item of result(mockAsyncIterableIterator(), {} as any)) {
res.push(item)
}

Expand All @@ -90,14 +96,14 @@ describe('#normalizeMigrateDefinition', () => {
describe('#createAsyncIterableMutation', () => {
it('should return an async iterable', async () => {
const mockMigration: NodeMigration = {
document: jest.fn() as any,
document: jest.fn<() => DocumentMigrationReturnValue>(),
}

const iterable = createAsyncIterableMutation(mockMigration, {documentTypes: ['foo']})

expect(typeof iterable).toBe('function')

const iterator = iterable(mockAsyncIterableIterator() as any, {} as any)
const iterator = iterable(mockAsyncIterableIterator(), {} as MigrationContext)
expect(typeof iterator.next).toBe('function')
expect(typeof iterator.return).toBe('function')
expect(typeof iterator.throw).toBe('function')
Expand Down

0 comments on commit 15c3bc1

Please sign in to comment.