Skip to content

Commit

Permalink
test(migration) add tests for normalizeMigrationDefinition (#5565)
Browse files Browse the repository at this point in the history
* test(migration) add tests for normalizeMigrationDefinition

* test(migration): fix type errors in tests

* test(migration): update test for normalizeMigrationDefinition
  • Loading branch information
binoy14 authored Feb 16, 2024
1 parent 4dedf3b commit c4baaab
Showing 1 changed file with 103 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import {
createAsyncIterableMutation,
normalizeMigrateDefinition,
} from '../normalizeMigrateDefinition'
import {Migration, NodeMigration} from '../../types'
import {createIfNotExists} from '../../mutations'

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

return {
next: jest.fn().mockImplementation(() => {

Check failure on line 13 in packages/@sanity/migrate/src/runner/__tests__/normalizeMigrationDefinition.test.ts

View workflow job for this annotation

GitHub Actions / typeCheck

Cannot find name 'jest'.

Check failure on line 13 in packages/@sanity/migrate/src/runner/__tests__/normalizeMigrationDefinition.test.ts

View workflow job for this annotation

GitHub Actions / typeCheck

Cannot find name 'jest'.
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) {

Check failure on line 19 in packages/@sanity/migrate/src/runner/__tests__/normalizeMigrationDefinition.test.ts

View workflow job for this annotation

GitHub Actions / typeCheck

Cannot find name 'jest'.

Check failure on line 19 in packages/@sanity/migrate/src/runner/__tests__/normalizeMigrationDefinition.test.ts

View workflow job for this annotation

GitHub Actions / typeCheck

Cannot find name 'jest'.
return this
}),
}
}

describe('#normalizeMigrateDefinition', () => {

Check failure on line 25 in packages/@sanity/migrate/src/runner/__tests__/normalizeMigrationDefinition.test.ts

View workflow job for this annotation

GitHub Actions / typeCheck

Cannot find name 'describe'. Do you need to install type definitions for a test runner? Try `npm i --save-dev @types/jest` or `npm i --save-dev @types/mocha`.

Check failure on line 25 in packages/@sanity/migrate/src/runner/__tests__/normalizeMigrationDefinition.test.ts

View workflow job for this annotation

GitHub Actions / typeCheck

Cannot find name 'describe'. Do you need to install type definitions for a test runner? Try `npm i --save-dev @types/jest` or `npm i --save-dev @types/mocha`.
it('should return the migrate is a function', async () => {

Check failure on line 26 in packages/@sanity/migrate/src/runner/__tests__/normalizeMigrationDefinition.test.ts

View workflow job for this annotation

GitHub Actions / typeCheck

Cannot find name 'it'. Do you need to install type definitions for a test runner? Try `npm i --save-dev @types/jest` or `npm i --save-dev @types/mocha`.

Check failure on line 26 in packages/@sanity/migrate/src/runner/__tests__/normalizeMigrationDefinition.test.ts

View workflow job for this annotation

GitHub Actions / typeCheck

Cannot find name 'it'. Do you need to install type definitions for a test runner? Try `npm i --save-dev @types/jest` or `npm i --save-dev @types/mocha`.
const mockMigration: Migration = {
title: 'mockMigration',
documentTypes: ['mockDocumentType'],
async *migrate() {
yield createIfNotExists({_type: 'mockDocumentType', _id: 'mockId'})
},
}

const result = normalizeMigrateDefinition(mockMigration)

const res = []
for await (const item of result(jest.fn(), {} as any)) {

Check failure on line 38 in packages/@sanity/migrate/src/runner/__tests__/normalizeMigrationDefinition.test.ts

View workflow job for this annotation

GitHub Actions / typeCheck

Cannot find name 'jest'.

Check failure on line 38 in packages/@sanity/migrate/src/runner/__tests__/normalizeMigrationDefinition.test.ts

View workflow job for this annotation

GitHub Actions / typeCheck

Cannot find name 'jest'.
res.push(item)
}

expect(res.flat()).toEqual([createIfNotExists({_type: 'mockDocumentType', _id: 'mockId'})])

Check failure on line 42 in packages/@sanity/migrate/src/runner/__tests__/normalizeMigrationDefinition.test.ts

View workflow job for this annotation

GitHub Actions / typeCheck

Cannot find name 'expect'.

Check failure on line 42 in packages/@sanity/migrate/src/runner/__tests__/normalizeMigrationDefinition.test.ts

View workflow job for this annotation

GitHub Actions / typeCheck

Cannot find name 'expect'.
})

it('should return a new mutations if migrate is not a function', async () => {

Check failure on line 45 in packages/@sanity/migrate/src/runner/__tests__/normalizeMigrationDefinition.test.ts

View workflow job for this annotation

GitHub Actions / typeCheck

Cannot find name 'it'. Do you need to install type definitions for a test runner? Try `npm i --save-dev @types/jest` or `npm i --save-dev @types/mocha`.

Check failure on line 45 in packages/@sanity/migrate/src/runner/__tests__/normalizeMigrationDefinition.test.ts

View workflow job for this annotation

GitHub Actions / typeCheck

Cannot find name 'it'. Do you need to install type definitions for a test runner? Try `npm i --save-dev @types/jest` or `npm i --save-dev @types/mocha`.
const mockMigration: Migration = {
title: 'mockMigration',
documentTypes: ['mockDocumentType'],
migrate: {
document() {
return createIfNotExists({_type: 'mockDocumentType', _id: 'mockId'})
},
},
}

const result = normalizeMigrateDefinition(mockMigration)
const res = []

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

expect(res.flat()).toEqual([createIfNotExists({_type: 'mockDocumentType', _id: 'mockId'})])

Check failure on line 63 in packages/@sanity/migrate/src/runner/__tests__/normalizeMigrationDefinition.test.ts

View workflow job for this annotation

GitHub Actions / typeCheck

Cannot find name 'expect'.

Check failure on line 63 in packages/@sanity/migrate/src/runner/__tests__/normalizeMigrationDefinition.test.ts

View workflow job for this annotation

GitHub Actions / typeCheck

Cannot find name 'expect'.
})

it('should not return undefined if migrate is returning undefined', async () => {

Check failure on line 66 in packages/@sanity/migrate/src/runner/__tests__/normalizeMigrationDefinition.test.ts

View workflow job for this annotation

GitHub Actions / typeCheck

Cannot find name 'it'. Do you need to install type definitions for a test runner? Try `npm i --save-dev @types/jest` or `npm i --save-dev @types/mocha`.

Check failure on line 66 in packages/@sanity/migrate/src/runner/__tests__/normalizeMigrationDefinition.test.ts

View workflow job for this annotation

GitHub Actions / typeCheck

Cannot find name 'it'. Do you need to install type definitions for a test runner? Try `npm i --save-dev @types/jest` or `npm i --save-dev @types/mocha`.
const mockMigration: Migration = {
title: 'mockMigration',
documentTypes: ['mockDocumentType'],
migrate: {
document() {
return undefined
},
},
}

const result = normalizeMigrateDefinition(mockMigration)
const res = []

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

expect(res.flat()).toEqual([])

Check failure on line 84 in packages/@sanity/migrate/src/runner/__tests__/normalizeMigrationDefinition.test.ts

View workflow job for this annotation

GitHub Actions / typeCheck

Cannot find name 'expect'.

Check failure on line 84 in packages/@sanity/migrate/src/runner/__tests__/normalizeMigrationDefinition.test.ts

View workflow job for this annotation

GitHub Actions / typeCheck

Cannot find name 'expect'.
})
})

describe('#createAsyncIterableMutation', () => {
it('should return an async iterable', async () => {
const mockMigration: NodeMigration = {
document: jest.fn(),
}

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

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

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

0 comments on commit c4baaab

Please sign in to comment.