|
|
@@ -1,8 +1,21 @@
|
|
|
import { LanguageCode } from '@vendure/common/lib/generated-types';
|
|
|
|
|
|
+import { Injector } from '../../common/injector';
|
|
|
+
|
|
|
import { validateCustomFieldValue } from './validate-custom-field-value';
|
|
|
|
|
|
describe('validateCustomFieldValue()', () => {
|
|
|
+ const injector = new Injector({} as any);
|
|
|
+
|
|
|
+ async function assertThrowsError(validateFn: () => Promise<void>, message: string) {
|
|
|
+ try {
|
|
|
+ await validateFn();
|
|
|
+ fail('Should have thrown');
|
|
|
+ } catch (e) {
|
|
|
+ expect(e.message).toBe(message);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
describe('string & localeString', () => {
|
|
|
const validate = (value: string) => () =>
|
|
|
validateCustomFieldValue(
|
|
|
@@ -12,18 +25,19 @@ describe('validateCustomFieldValue()', () => {
|
|
|
pattern: '^[0-9]+',
|
|
|
},
|
|
|
value,
|
|
|
+ injector,
|
|
|
);
|
|
|
|
|
|
- it('passes valid pattern', () => {
|
|
|
- expect(validate('1')).not.toThrow();
|
|
|
- expect(validate('123')).not.toThrow();
|
|
|
- expect(validate('1foo')).not.toThrow();
|
|
|
+ it('passes valid pattern', async () => {
|
|
|
+ expect(await validate('1')).not.toThrow();
|
|
|
+ expect(await validate('123')).not.toThrow();
|
|
|
+ expect(await validate('1foo')).not.toThrow();
|
|
|
});
|
|
|
|
|
|
- it('throws on invalid pattern', () => {
|
|
|
- expect(validate('')).toThrowError('error.field-invalid-string-pattern');
|
|
|
- expect(validate('foo')).toThrowError('error.field-invalid-string-pattern');
|
|
|
- expect(validate(' 1foo')).toThrowError('error.field-invalid-string-pattern');
|
|
|
+ it('throws on invalid pattern', async () => {
|
|
|
+ await assertThrowsError(validate(''), 'error.field-invalid-string-pattern');
|
|
|
+ await assertThrowsError(validate('foo'), 'error.field-invalid-string-pattern');
|
|
|
+ await assertThrowsError(validate(' 1foo'), 'error.field-invalid-string-pattern');
|
|
|
});
|
|
|
});
|
|
|
|
|
|
@@ -36,17 +50,18 @@ describe('validateCustomFieldValue()', () => {
|
|
|
options: [{ value: 'small' }, { value: 'large' }],
|
|
|
},
|
|
|
value,
|
|
|
+ injector,
|
|
|
);
|
|
|
|
|
|
- it('passes valid option', () => {
|
|
|
- expect(validate('small')).not.toThrow();
|
|
|
- expect(validate('large')).not.toThrow();
|
|
|
+ it('passes valid option', async () => {
|
|
|
+ expect(await validate('small')).not.toThrow();
|
|
|
+ expect(await validate('large')).not.toThrow();
|
|
|
});
|
|
|
|
|
|
- it('throws on invalid option', () => {
|
|
|
- expect(validate('SMALL')).toThrowError('error.field-invalid-string-option');
|
|
|
- expect(validate('')).toThrowError('error.field-invalid-string-option');
|
|
|
- expect(validate('bad')).toThrowError('error.field-invalid-string-option');
|
|
|
+ it('throws on invalid option', async () => {
|
|
|
+ await assertThrowsError(validate('SMALL'), 'error.field-invalid-string-option');
|
|
|
+ await assertThrowsError(validate(''), 'error.field-invalid-string-option');
|
|
|
+ await assertThrowsError(validate('bad'), 'error.field-invalid-string-option');
|
|
|
});
|
|
|
});
|
|
|
|
|
|
@@ -60,18 +75,19 @@ describe('validateCustomFieldValue()', () => {
|
|
|
max: 10,
|
|
|
},
|
|
|
value,
|
|
|
+ injector,
|
|
|
);
|
|
|
|
|
|
- it('passes valid range', () => {
|
|
|
- expect(validate(5)).not.toThrow();
|
|
|
- expect(validate(7)).not.toThrow();
|
|
|
- expect(validate(10)).not.toThrow();
|
|
|
+ it('passes valid range', async () => {
|
|
|
+ expect(await validate(5)).not.toThrow();
|
|
|
+ expect(await validate(7)).not.toThrow();
|
|
|
+ expect(await validate(10)).not.toThrow();
|
|
|
});
|
|
|
|
|
|
- it('throws on invalid range', () => {
|
|
|
- expect(validate(4)).toThrowError('error.field-invalid-number-range-min');
|
|
|
- expect(validate(11)).toThrowError('error.field-invalid-number-range-max');
|
|
|
- expect(validate(-7)).toThrowError('error.field-invalid-number-range-min');
|
|
|
+ it('throws on invalid range', async () => {
|
|
|
+ await assertThrowsError(validate(4), 'error.field-invalid-number-range-min');
|
|
|
+ await assertThrowsError(validate(11), 'error.field-invalid-number-range-max');
|
|
|
+ await assertThrowsError(validate(-7), 'error.field-invalid-number-range-min');
|
|
|
});
|
|
|
});
|
|
|
|
|
|
@@ -85,19 +101,22 @@ describe('validateCustomFieldValue()', () => {
|
|
|
max: '2019-06-01T08:30',
|
|
|
},
|
|
|
value,
|
|
|
+ injector,
|
|
|
);
|
|
|
|
|
|
- it('passes valid range', () => {
|
|
|
- expect(validate('2019-01-01T08:30:00.000')).not.toThrow();
|
|
|
- expect(validate('2019-06-01T08:30:00.000')).not.toThrow();
|
|
|
- expect(validate('2019-04-12T14:15:51.200')).not.toThrow();
|
|
|
+ it('passes valid range', async () => {
|
|
|
+ expect(await validate('2019-01-01T08:30:00.000')).not.toThrow();
|
|
|
+ expect(await validate('2019-06-01T08:30:00.000')).not.toThrow();
|
|
|
+ expect(await validate('2019-04-12T14:15:51.200')).not.toThrow();
|
|
|
});
|
|
|
|
|
|
- it('throws on invalid range', () => {
|
|
|
- expect(validate('2019-01-01T08:29:00.000')).toThrowError(
|
|
|
+ it('throws on invalid range', async () => {
|
|
|
+ await assertThrowsError(
|
|
|
+ validate('2019-01-01T08:29:00.000'),
|
|
|
'error.field-invalid-datetime-range-min',
|
|
|
);
|
|
|
- expect(validate('2019-06-01T08:30:00.100')).toThrowError(
|
|
|
+ await assertThrowsError(
|
|
|
+ validate('2019-06-01T08:30:00.100'),
|
|
|
'error.field-invalid-datetime-range-max',
|
|
|
);
|
|
|
});
|
|
|
@@ -116,6 +135,7 @@ describe('validateCustomFieldValue()', () => {
|
|
|
},
|
|
|
},
|
|
|
value,
|
|
|
+ injector,
|
|
|
);
|
|
|
const validate2 = (value: string, languageCode: LanguageCode) => () =>
|
|
|
validateCustomFieldValue(
|
|
|
@@ -132,27 +152,28 @@ describe('validateCustomFieldValue()', () => {
|
|
|
},
|
|
|
},
|
|
|
value,
|
|
|
+ injector,
|
|
|
languageCode,
|
|
|
);
|
|
|
|
|
|
- it('passes validate fn string', () => {
|
|
|
- expect(validate1('valid')).not.toThrow();
|
|
|
+ it('passes validate fn string', async () => {
|
|
|
+ expect(await validate1('valid')).not.toThrow();
|
|
|
});
|
|
|
|
|
|
- it('passes validate fn localized string', () => {
|
|
|
- expect(validate2('valid', LanguageCode.de)).not.toThrow();
|
|
|
+ it('passes validate fn localized string', async () => {
|
|
|
+ expect(await validate2('valid', LanguageCode.de)).not.toThrow();
|
|
|
});
|
|
|
|
|
|
- it('fails validate fn string', () => {
|
|
|
- expect(validate1('bad')).toThrowError('invalid');
|
|
|
+ it('fails validate fn string', async () => {
|
|
|
+ await assertThrowsError(validate1('bad'), 'invalid');
|
|
|
});
|
|
|
|
|
|
- it('fails validate fn localized string en', () => {
|
|
|
- expect(validate2('bad', LanguageCode.en)).toThrowError('invalid');
|
|
|
+ it('fails validate fn localized string en', async () => {
|
|
|
+ await assertThrowsError(validate2('bad', LanguageCode.en), 'invalid');
|
|
|
});
|
|
|
|
|
|
- it('fails validate fn localized string de', () => {
|
|
|
- expect(validate2('bad', LanguageCode.de)).toThrowError('ungültig');
|
|
|
+ it('fails validate fn localized string de', async () => {
|
|
|
+ await assertThrowsError(validate2('bad', LanguageCode.de), 'ungültig');
|
|
|
});
|
|
|
});
|
|
|
});
|