summaryrefslogtreecommitdiff
path: root/spec/frontend/__helpers__/matchers/to_validate_json_schema_spec.js
blob: e6096221528ccf1cbbd67b1b9804f862c8050d52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import Ajv from 'ajv';
import AjvFormats from 'ajv-formats';

const JSON_SCHEMA = {
  type: 'object',
  properties: {
    fruit: {
      type: 'string',
      minLength: 3,
    },
  },
};

const ajv = new Ajv({
  strictTypes: false,
  strictTuples: false,
  allowMatchingProperties: true,
});

AjvFormats(ajv);
const schema = ajv.compile(JSON_SCHEMA);

describe('custom matcher toValidateJsonSchema', () => {
  it('throws error if validator is not compiled correctly', () => {
    expect(() => {
      expect({}).toValidateJsonSchema({});
    }).toThrow(
      'Validator must be a validating function with property "schema", created with `ajv.compile`. See https://ajv.js.org/api.html#ajv-compile-schema-object-data-any-boolean-promise-any.',
    );
  });

  describe('positive assertions', () => {
    it.each`
      description      | input
      ${'valid input'} | ${{ fruit: 'apple' }}
    `('schema validation passes for $description', ({ input }) => {
      expect(input).toValidateJsonSchema(schema);
    });

    it('throws if not matching', () => {
      expect(() => expect(null).toValidateJsonSchema(schema)).toThrow(
        `Expected the given data to pass the schema validation, but found that it was considered invalid. Errors:
Error with item : must be object`,
      );
    });
  });

  describe('negative assertions', () => {
    it.each`
      description                    | input
      ${'no input'}                  | ${null}
      ${'input with invalid type'}   | ${'banana'}
      ${'input with invalid length'} | ${{ fruit: 'aa' }}
      ${'input with invalid type'}   | ${{ fruit: 12345 }}
    `('schema validation fails for $description', ({ input }) => {
      expect(input).not.toValidateJsonSchema(schema);
    });

    it('throws if matching', () => {
      expect(() => expect({ fruit: 'apple' }).not.toValidateJsonSchema(schema)).toThrow(
        'Expected the given data not to pass the schema validation, but found that it was considered valid.',
      );
    });
  });
});