Skip to content

Commit

Permalink
Merge branch 'master' of github.com:vazco/uniforms into stricter-types
Browse files Browse the repository at this point in the history
  • Loading branch information
radekmie committed Jan 10, 2023
2 parents b62aa27 + 8bb920f commit 46c3ed2
Show file tree
Hide file tree
Showing 11 changed files with 31 additions and 327 deletions.
153 changes: 0 additions & 153 deletions packages/uniforms-bridge-graphql/__tests__/GraphQLBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,59 +260,6 @@ describe('GraphQLBridge', () => {
placeholder: 'Post ID',
required: true,
});
expect(bridgeT.getProps('id', { label: true })).toEqual({
allowedValues: [1, 2, 3],
label: 'Post ID',
placeholder: 'Post ID',
required: true,
});
});
});
});

describe('when props.label is a string', () => {
it('should use label from props', () => {
expect(bridgeT.getProps('title', { label: 'Overriden' })).toEqual({
allowedValues: ['a', 'b', 'Some Title'],
label: false,
required: false,
transform: expect.any(Function),
options: [
{ label: 1, value: 'a' },
{ label: 2, value: 'b' },
{ label: 3, value: 'Some Title' },
],
initialValue: 'Some Title',
});
expect(bridgeT.getProps('title', { label: '' })).toEqual({
allowedValues: ['a', 'b', 'Some Title'],
label: false,
required: false,
transform: expect.any(Function),
options: [
{ label: 1, value: 'a' },
{ label: 2, value: 'b' },
{ label: 3, value: 'Some Title' },
],
initialValue: 'Some Title',
});
});
});

describe('when props.label is false or null (unset)', () => {
it('should display empty label', () => {
expect(bridgeT.getProps('id', { label: false })).toEqual({
allowedValues: [1, 2, 3],
label: 'Post ID',
placeholder: 'Post ID',
required: true,
});

expect(bridgeT.getProps('id', { label: null })).toEqual({
allowedValues: [1, 2, 3],
label: 'Post ID',
placeholder: 'Post ID',
required: true,
});
});
});
Expand All @@ -327,15 +274,6 @@ describe('GraphQLBridge', () => {
});
});

it('works with allowedValues from props', () => {
expect(bridgeI.getProps('id', { allowedValues: [1] })).toEqual({
label: 'Post ID',
placeholder: 'Post ID',
required: true,
allowedValues: [1, 2, 3],
});
});

it('works with custom component', () => {
expect(bridgeI.getProps('author')).toEqual({
label: 'Author',
Expand All @@ -344,75 +282,6 @@ describe('GraphQLBridge', () => {
});
});

it('works with label (custom)', () => {
expect(bridgeI.getProps('id', { label: 'ID' })).toEqual({
label: 'Post ID',
placeholder: 'Post ID',
required: true,
allowedValues: [1, 2, 3],
});
});

it('works with label (true)', () => {
expect(bridgeI.getProps('id', { label: true })).toEqual({
label: 'Post ID',
placeholder: 'Post ID',
required: true,
allowedValues: [1, 2, 3],
});
});

it('works with label (falsy)', () => {
expect(bridgeI.getProps('id', { label: null })).toEqual({
label: 'Post ID',
placeholder: 'Post ID',
required: true,
allowedValues: [1, 2, 3],
});
});

it('works with placeholder (custom)', () => {
expect(bridgeI.getProps('id', { placeholder: 'Post ID' })).toEqual({
label: 'Post ID',
placeholder: 'Post ID',
required: true,
allowedValues: [1, 2, 3],
});
});

it('works with placeholder (true)', () => {
expect(bridgeI.getProps('id', { placeholder: true })).toEqual({
label: 'Post ID',
placeholder: 'Post ID',
required: true,
allowedValues: [1, 2, 3],
});
});

it('works with placeholder (falsy)', () => {
expect(bridgeI.getProps('id', { placeholder: null })).toEqual({
label: 'Post ID',
placeholder: 'Post ID',
required: true,
allowedValues: [1, 2, 3],
});
});

it('works with placeholder (extra.placeholder === undefined)', () => {
expect(bridgeI.getProps('title', { placeholder: true })).toEqual({
allowedValues: ['a', 'b', 'Some Title'],
label: false,
required: false,
transform: expect.any(Function),
options: [
{ label: 1, value: 'a' },
{ label: 2, value: 'b' },
{ label: 3, value: 'Some Title' },
],
initialValue: 'Some Title',
});
});

it('works with Number type', () => {
expect(bridgeI.getProps('author.decimal1')).toEqual({
label: 'Decimal 1',
Expand Down Expand Up @@ -441,28 +310,6 @@ describe('GraphQLBridge', () => {
expect(bridgeI.getProps('votes').allowedValues[1]).toBe('b');
});

it('works with options from props', () => {
expect(
bridgeI.getProps('votes', { options: { c: 1, d: 2 } }).transform('c'),
).toBe(1);
expect(
bridgeI.getProps('votes', { options: { c: 1, d: 2 } }).transform('d'),
).toBe(2);
expect(
bridgeI.getProps('votes', { options: { c: 1, d: 2 } }).allowedValues[0],
).toBe('c');
expect(
bridgeI.getProps('votes', { options: { c: 1, d: 2 } }).allowedValues[1],
).toBe('d');
});

it('works with other props', () => {
expect(bridgeI.getProps('category', { x: 1, y: 1 })).toEqual({
label: 'Category',
required: true,
});
});

describe('when enum', () => {
it('should return possibleValues', () => {
expect(bridgeI.getProps('author.level').allowedValues).toEqual([
Expand Down
5 changes: 3 additions & 2 deletions packages/uniforms-bridge-graphql/src/GraphQLBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default class GraphQLBridge extends Bridge {
// Memoize for performance and referential equality.
this.getField = memoize(this.getField.bind(this));
this.getInitialValue = memoize(this.getInitialValue.bind(this));
this.getProps = memoize(this.getProps.bind(this));
this.getSubfields = memoize(this.getSubfields.bind(this));
this.getType = memoize(this.getType.bind(this));
}
Expand Down Expand Up @@ -107,7 +108,7 @@ export default class GraphQLBridge extends Bridge {
return defaultValue ?? this.extras[name]?.initialValue;
}

getProps(nameNormal: string, fieldProps?: UnknownObject) {
getProps(nameNormal: string) {
const nameGeneric = nameNormal.replace(/\.\d+/g, '.$');

const field = this.getField(nameGeneric);
Expand All @@ -128,7 +129,7 @@ export default class GraphQLBridge extends Bridge {
type OptionDict = Record<string, string>;
type OptionList = { label: string; value: unknown }[];
type Options = OptionDict | OptionList;
const options: Options = fieldProps?.options || props.options;
const options: Options = props.options;
if (options) {
if (Array.isArray(options)) {
props.allowedValues = options.map(option => option.value);
Expand Down
91 changes: 5 additions & 86 deletions packages/uniforms-bridge-json-schema/__tests__/JSONSchemaBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -673,17 +673,7 @@ describe('JSONSchemaBridge', () => {
});
});

it('works with allowedValues from props', () => {
expect(
bridge.getProps('shippingAddress.type', { allowedValues: [1] }),
).toEqual({
allowedValues: ['residential', 'business'],
label: 'Type',
required: true,
});
});

it('works with allowedValues from props', () => {
it('works with custom props', () => {
expect(bridge.getProps('forcedRequired')).toEqual({
label: 'Forced required',
required: true,
Expand All @@ -705,67 +695,6 @@ describe('JSONSchemaBridge', () => {
});
});

it('works with label (custom)', () => {
expect(bridge.getProps('dateOfBirth', { label: 'Death' })).toEqual({
label: 'Date of birth',
required: true,
});
});

it('works with label (true)', () => {
expect(bridge.getProps('dateOfBirth', { label: true })).toEqual({
label: 'Date of birth',
required: true,
});
});

it('works with property title as default label', () => {
expect(bridge.getProps('hasAJob', { label: true })).toEqual({
allowedValues: undefined,
label: 'Currently Employed',
options: undefined,
placeholder: undefined,
required: false,
});
});

it('works with placeholder (custom)', () => {
expect(bridge.getProps('email.work', { placeholder: 'Email' })).toEqual({
label: 'Work',
required: true,
});
});

it('works with placeholder (true)', () => {
expect(bridge.getProps('email.work', { placeholder: true })).toEqual({
label: 'Work',
required: true,
});
});

it('works with placeholder (falsy)', () => {
expect(bridge.getProps('email.work', { placeholder: null })).toEqual({
label: 'Work',
required: true,
});
});

it('works with placeholder (label falsy)', () => {
expect(
bridge.getProps('email.work', { label: null, placeholder: true }),
).toEqual({
label: 'Work',
required: true,
});

expect(
bridge.getProps('email.work', { label: false, placeholder: true }),
).toEqual({
label: 'Work',
required: true,
});
});

it('works with Number type', () => {
expect(bridge.getProps('salary')).toEqual({
allowedValues: ['low', 'medium', 'height'],
Expand Down Expand Up @@ -799,14 +728,6 @@ describe('JSONSchemaBridge', () => {
expect(bridge.getProps('salary').allowedValues[1]).toBe('medium');
});

it('works with options from props', () => {
const props = { options: { minimal: 4000, avarage: 8000 } };
expect(bridge.getProps('salary', props).transform('minimal')).toBe(4000);
expect(bridge.getProps('salary', props).transform('avarage')).toBe(8000);
expect(bridge.getProps('salary', props).allowedValues[0]).toBe('minimal');
expect(bridge.getProps('salary', props).allowedValues[1]).toBe('avarage');
});

it('works with type', () => {
expect(bridge.getProps('password')).toEqual({
label: 'Password',
Expand All @@ -826,12 +747,10 @@ describe('JSONSchemaBridge', () => {
});

it('works with other props', () => {
expect(bridge.getProps('personalData.firstName', { x: 1, y: 1 })).toEqual(
{
label: 'First name',
required: false,
},
);
expect(bridge.getProps('personalData.firstName')).toEqual({
label: 'First name',
required: false,
});
});

it('works with allOf in items', () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/uniforms-bridge-json-schema/src/JSONSchemaBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ export default class JSONSchemaBridge extends Bridge {
return undefined;
}

getProps(name: string, fieldProps?: UnknownObject) {
getProps(name: string) {
const field = this.getField(name);
const props = Object.assign(
{},
Expand Down Expand Up @@ -278,7 +278,7 @@ export default class JSONSchemaBridge extends Bridge {
type OptionDict = Record<string, string>;
type OptionList = { label: string; value: unknown }[];
type Options = OptionDict | OptionList;
const options: Options = fieldProps?.options || props.options;
const options: Options = props.options;
if (options) {
if (Array.isArray(options)) {
props.allowedValues = options.map(option => option.value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,6 @@ describe('SimpleSchema2Bridge', () => {
});
});

it('works with allowedValues from props', () => {
expect(bridge.getProps('o', { allowedValues: ['O'] })).toEqual({
label: 'O',
required: true,
});
});

it('works with custom component', () => {
expect(bridge.getProps('l')).toEqual({
label: 'L',
Expand Down Expand Up @@ -246,21 +239,6 @@ describe('SimpleSchema2Bridge', () => {
expect(bridge.getProps('r').allowedValues[1]).toBe('b');
});

it('works with options from props', () => {
expect(
bridge.getProps('s', { options: { c: 1, d: 2 } }).transform('c'),
).toBe(1);
expect(
bridge.getProps('s', { options: { c: 1, d: 2 } }).transform('d'),
).toBe(2);
expect(
bridge.getProps('s', { options: { c: 1, d: 2 } }).allowedValues[0],
).toBe('c');
expect(
bridge.getProps('s', { options: { c: 1, d: 2 } }).allowedValues[1],
).toBe('d');
});

it('works with transform', () => {
expect(bridge.getProps('p')).toEqual({
label: 'P',
Expand All @@ -269,13 +247,6 @@ describe('SimpleSchema2Bridge', () => {
});
});

it('works with transform from props', () => {
expect(bridge.getProps('p', { transform: () => {} })).toEqual({
label: 'P',
required: true,
});
});

it('works with type', () => {
expect(bridge.getProps('aa')).toEqual({
label: 'Aa',
Expand Down
Loading

0 comments on commit 46c3ed2

Please sign in to comment.