-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add validation helper modify funcs and options for addInValues (#30)
Also add the unit test setup
- Loading branch information
Showing
10 changed files
with
6,278 additions
and
596 deletions.
There are no files selected for viewing
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
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
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
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 |
---|---|---|
@@ -1,10 +1,22 @@ | ||
import type Migration, { MigrationContext } from "contentful-migration"; | ||
import type Migration from "contentful-migration"; | ||
import type { MigrationContext } from "contentful-migration"; | ||
|
||
export type ValueMappingFunction = (values: string[]) => string[]; | ||
|
||
export type AddValuesOptionMode = 'sorted' | 'start' | 'end' | 'before' | 'after'; | ||
|
||
export interface AddValuesOptions { | ||
mode?: AddValuesOptionMode; | ||
ref?: string; | ||
} | ||
|
||
export interface ValidationHelpers { | ||
addLinkContentTypeValues(contentTypeId: string, fieldId: string, values: string | [string]): Promise<void>; | ||
addInValues(contentTypeId: string, fieldId: string, values: string | [string]): Promise<void>; | ||
removeLinkContentTypeValues(contentTypeId: string, fieldId: string, values: string | [string]): Promise<void>; | ||
removeInValues(contentTypeId: string, fieldId: string, values: string | [string]): Promise<void>; | ||
addLinkContentTypeValues(contentTypeId: string, fieldId: string, values: string | string[]): Promise<void>; | ||
addInValues(contentTypeId: string, fieldId: string, values: string | string[], options?: AddValuesOptions): Promise<void>; | ||
removeLinkContentTypeValues(contentTypeId: string, fieldId: string, values: string | string[]): Promise<void>; | ||
removeInValues(contentTypeId: string, fieldId: string, values: string | string[]): Promise<void>; | ||
modifyLinkContentTypeValues(contentTypeId: string, fieldId: string, valueMappingFunction: ValueMappingFunction): Promise<void>; | ||
modifyInValues(contentTypeId: string, fieldId: string, valueMappingFunction: ValueMappingFunction): Promise<void>; | ||
} | ||
|
||
export function getValidationHelpers(migration: Migration, context: MigrationContext): ValidationHelpers; |
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
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,171 @@ | ||
const { getValidationHelpers } = require('./validation'); | ||
|
||
describe('getValidationHelpers', () => { | ||
const getTestContentType = () => ({ | ||
fields: [ | ||
{ | ||
id: 'selectWithOtherValidationProps', | ||
type: 'Text', | ||
items: [], | ||
validations: [ | ||
{ | ||
foo: 'test', | ||
}, | ||
{ | ||
in: ['foo', 'bar', 'baz'], | ||
message: 'Some message', | ||
}, | ||
], | ||
}, | ||
{ | ||
id: 'select', | ||
type: 'Text', | ||
items: [], | ||
validations: [ | ||
{ | ||
in: ['foo', 'bar', 'baz'], | ||
}, | ||
], | ||
}, | ||
{ | ||
id: 'reference', | ||
type: 'Text', | ||
validations: [ | ||
{ | ||
linkContentType: ['foo', 'bar', 'baz'], | ||
}, | ||
], | ||
}, | ||
{ | ||
id: 'string-array', | ||
type: 'Array', | ||
items: [], | ||
}, | ||
], | ||
}); | ||
|
||
const context = { | ||
makeRequest: () => Promise.resolve(getTestContentType()), | ||
}; | ||
|
||
let resultValidation; | ||
|
||
const migration = { | ||
editContentType: () => ({ | ||
editField: () => ({ | ||
validations: (validationObject) => { | ||
resultValidation = validationObject; | ||
}, | ||
}), | ||
}), | ||
}; | ||
|
||
beforeEach(() => { | ||
resultValidation = undefined; | ||
}); | ||
|
||
describe('addInValues', () => { | ||
it('should add values at the end', async () => { | ||
const validations = getValidationHelpers(migration, context); | ||
await validations.addInValues('some-content-type', 'selectWithOtherValidationProps', 'bat'); | ||
expect(resultValidation).toEqual([ | ||
{ | ||
foo: 'test', | ||
}, | ||
{ | ||
in: ['foo', 'bar', 'baz', 'bat'], | ||
message: 'Some message', | ||
}, | ||
]); | ||
}); | ||
|
||
it('should only add new values', async () => { | ||
const validations = getValidationHelpers(migration, context); | ||
await validations.addInValues('some-content-type', 'select', ['bat', 'foo', 'test']); | ||
expect(resultValidation).toEqual([ | ||
{ | ||
in: ['foo', 'bar', 'baz', 'bat', 'test'], | ||
}, | ||
]); | ||
}); | ||
|
||
it('should add new values sorted', async () => { | ||
const validations = getValidationHelpers(migration, context); | ||
await validations.addInValues('some-content-type', 'select', ['bat', 'foo', 'test'], { mode: 'sorted' }); | ||
expect(resultValidation).toEqual([ | ||
{ | ||
in: ['bar', 'bat', 'baz', 'foo', 'test'], | ||
}, | ||
]); | ||
}); | ||
}); | ||
|
||
describe('removeInValues', () => { | ||
it('should remove values', async () => { | ||
const validations = getValidationHelpers(migration, context); | ||
await validations.removeInValues('some-content-type', 'select', ['foo', 'baz']); | ||
expect(resultValidation).toEqual([ | ||
{ | ||
in: ['bar'], | ||
}, | ||
]); | ||
}); | ||
}); | ||
|
||
describe('modifyInValues', () => { | ||
it('should modify values with custom function', async () => { | ||
const validations = getValidationHelpers(migration, context); | ||
await validations.modifyInValues('some-content-type', 'select', (values) => { | ||
const result = values.slice(0, values.length - 1); // remove bar | ||
result.push('test'); | ||
return result; | ||
}); | ||
expect(resultValidation).toEqual([ | ||
{ | ||
in: ['foo', 'bar', 'test'], | ||
}, | ||
]); | ||
}); | ||
}); | ||
|
||
describe('addLinkContentTypeValues', () => { | ||
it('should add values at the end', async () => { | ||
const validations = getValidationHelpers(migration, context); | ||
await validations.addLinkContentTypeValues('some-content-type', 'reference', 'bat'); | ||
expect(resultValidation).toEqual([ | ||
{ | ||
linkContentType: ['foo', 'bar', 'baz', 'bat'], | ||
}, | ||
]); | ||
}); | ||
}); | ||
|
||
describe('removeLinkContentTypeValues', () => { | ||
it('should remove values', async () => { | ||
const validations = getValidationHelpers(migration, context); | ||
await validations.removeLinkContentTypeValues('some-content-type', 'reference', ['foo', 'baz']); | ||
expect(resultValidation).toEqual([ | ||
{ | ||
linkContentType: ['bar'], | ||
}, | ||
]); | ||
}); | ||
}); | ||
|
||
describe('modifyLinkContentTypeValues', () => { | ||
it('should modify unique values with custom function', async () => { | ||
const validations = getValidationHelpers(migration, context); | ||
await validations.modifyLinkContentTypeValues('some-content-type', 'reference', (values) => { | ||
const result = values.slice(0, values.length - 1); // remove bar | ||
result.push('test'); | ||
result.push('foo'); // should be removed since it exists | ||
return result; | ||
}); | ||
expect(resultValidation).toEqual([ | ||
{ | ||
linkContentType: ['foo', 'bar', 'test'], | ||
}, | ||
]); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.