-
Notifications
You must be signed in to change notification settings - Fork 432
Commit
* test(migration) add tests for normalizeMigrationDefinition * test(migration): fix type errors in tests * test(migration): update test for normalizeMigrationDefinition
- Loading branch information
There are no files selected for viewing
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 GitHub Actions / typeCheck
|
||
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 GitHub Actions / typeCheck
|
||
return this | ||
}), | ||
} | ||
} | ||
|
||
describe('#normalizeMigrateDefinition', () => { | ||
Check failure on line 25 in packages/@sanity/migrate/src/runner/__tests__/normalizeMigrationDefinition.test.ts GitHub Actions / typeCheck
Check failure on line 25 in packages/@sanity/migrate/src/runner/__tests__/normalizeMigrationDefinition.test.ts GitHub Actions / typeCheck
|
||
it('should return the migrate is a function', async () => { | ||
Check failure on line 26 in packages/@sanity/migrate/src/runner/__tests__/normalizeMigrationDefinition.test.ts GitHub Actions / typeCheck
Check failure on line 26 in packages/@sanity/migrate/src/runner/__tests__/normalizeMigrationDefinition.test.ts GitHub Actions / typeCheck
|
||
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 GitHub Actions / typeCheck
|
||
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 GitHub Actions / typeCheck
|
||
}) | ||
|
||
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 GitHub Actions / typeCheck
Check failure on line 45 in packages/@sanity/migrate/src/runner/__tests__/normalizeMigrationDefinition.test.ts GitHub Actions / typeCheck
|
||
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 GitHub Actions / typeCheck
|
||
}) | ||
|
||
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 GitHub Actions / typeCheck
Check failure on line 66 in packages/@sanity/migrate/src/runner/__tests__/normalizeMigrationDefinition.test.ts GitHub Actions / typeCheck
|
||
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 GitHub Actions / typeCheck
|
||
}) | ||
}) | ||
|
||
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') | ||
}) | ||
}) |