-
Notifications
You must be signed in to change notification settings - Fork 432
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(migration) add tests for normalizeMigrationDefinition
- Loading branch information
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
packages/@sanity/migrate/src/runner/__tests__/normalizeMigrationDefinition.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,65 @@ | ||
import { | ||
createAsyncIterableMutation, | ||
normalizeMigrateDefinition, | ||
} from '../normalizeMigrateDefinition' | ||
import {Migration, NodeMigration} from '../../types' | ||
|
||
describe('#normalizeMigrateDefinition', () => { | ||
it('should return the migrate function if it is a function', () => { | ||
const mockMigration: Migration = { | ||
name: 'mockMigration', | ||
documentType: 'mockDocumentType', | ||
migrate: jest.fn(), | ||
} | ||
|
||
const result = normalizeMigrateDefinition(mockMigration) | ||
expect(result).toBe(mockMigration.migrate) | ||
}) | ||
|
||
it('should return a new function if migrate is not a function', () => { | ||
const mockMigration: Migration = { | ||
name: 'mockMigration', | ||
documentType: 'mockDocumentType', | ||
migrate: {}, | ||
} | ||
|
||
const result = normalizeMigrateDefinition(mockMigration) | ||
expect(typeof result).toBe('function') | ||
}) | ||
}) | ||
|
||
describe('#createAsyncIterableMutation', () => { | ||
const mockAsyncIterableIterator = () => { | ||
const data = ['item1', 'item2', 'item3'] // Sample data to be returned by the iterator | ||
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 | ||
}), | ||
} | ||
} | ||
|
||
it('should return an async iterable', async () => { | ||
const mockMigration: NodeMigration = { | ||
document: jest.fn(), | ||
} | ||
|
||
const iterable = createAsyncIterableMutation(mockMigration, {documentType: 'foo'}) | ||
|
||
expect(typeof iterable).toBe('function') | ||
|
||
const iterator = iterable(mockAsyncIterableIterator(), { | ||
withDocument: jest.fn(), | ||
}) | ||
expect(typeof iterator.next).toBe('function') | ||
expect(typeof iterator.return).toBe('function') | ||
expect(typeof iterator.throw).toBe('function') | ||
}) | ||
}) |