@opra/testing 0.29.0 → 0.30.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -38,65 +38,6 @@ expect.extend({
38
38
  message: () => 'Value is not an array'
39
39
  };
40
40
  },
41
- toBeSorted(received, compareFn) {
42
- let pass = true;
43
- let message;
44
- if (pass) {
45
- const sorted = [...(received || [])];
46
- sorted.sort(compareFn);
47
- try {
48
- expect(received).toEqual(sorted);
49
- }
50
- catch (e) {
51
- pass = false;
52
- message = () => 'Array items is not sorted as expected';
53
- }
54
- }
55
- return {
56
- actual: received,
57
- message,
58
- pass
59
- };
60
- },
61
- toBeSortedBy(received, properties) {
62
- const fieldsMap = properties.map(x => x.split('.'));
63
- const getValue = (obj, fieldMap) => {
64
- let v = obj;
65
- let i = 0;
66
- while (v && i < fieldMap.length) {
67
- v = v[fieldMap[i++]];
68
- }
69
- return v;
70
- };
71
- let pass = true;
72
- let message;
73
- if (pass) {
74
- const sorted = [...(received || [])];
75
- sorted.sort((a, b) => {
76
- for (const sortField of fieldsMap) {
77
- const l = getValue(a, sortField);
78
- const r = getValue(b, sortField);
79
- if (l < r)
80
- return -1;
81
- if (l > r)
82
- return 1;
83
- }
84
- return 0;
85
- });
86
- try {
87
- expect(received).toEqual(sorted);
88
- }
89
- catch (e) {
90
- pass = false;
91
- message = () => 'Array items is not sorted as expected';
92
- }
93
- }
94
- return {
95
- actual: received,
96
- message,
97
- pass
98
- };
99
- },
100
41
  toBeGreaterThanAny(received, expected) {
101
42
  return compare('toBeGreaterThan', {
102
43
  isNot: this.isNot,
@@ -20,13 +20,15 @@ function _objectMatches(received, expected, path) {
20
20
  throw new Error(`Property "${k}" does not match`);
21
21
  }
22
22
  }
23
- if (ev && typeof ev === 'object')
23
+ else if (ev && typeof ev === 'object') {
24
24
  _objectMatches(rv, ev, path ? path + '.' + k : k);
25
- try {
26
- expect(rv).toStrictEqual(ev);
27
- }
28
- catch {
29
- throw new Error(`Property "${k}" does not match`);
30
25
  }
26
+ else
27
+ try {
28
+ expect(rv).toEqual(ev);
29
+ }
30
+ catch {
31
+ throw new Error(`Property "${k}" does not match`);
32
+ }
31
33
  }
32
34
  }
@@ -0,0 +1,13 @@
1
+ import '../jest-extend/index.js';
2
+ export class ApiExpectBase {
3
+ constructor(response, isNot) {
4
+ this.response = response;
5
+ this.isNot = isNot;
6
+ }
7
+ _expect(expected) {
8
+ const out = expect(expected);
9
+ if (this.isNot)
10
+ return out.not;
11
+ return out;
12
+ }
13
+ }
@@ -1,28 +1,61 @@
1
- import '../jest-extend/index.js';
2
1
  import isNil from 'lodash.isnil';
3
2
  import omitBy from 'lodash.omitby';
4
3
  import ruleJudgmentLib from 'rule-judgment';
5
4
  import { OpraFilter } from '@opra/common';
5
+ import { ApiExpectBase } from './api-expect-base.js';
6
6
  // @ts-ignore
7
7
  const ruleJudgment = typeof ruleJudgmentLib === 'object' ? ruleJudgmentLib.default : ruleJudgmentLib;
8
- export class ApiExpectCollection {
9
- constructor(response, _isNot = false) {
10
- this.response = response;
11
- this._isNot = _isNot;
12
- }
8
+ export class ApiExpectCollection extends ApiExpectBase {
13
9
  get not() {
14
- return new ApiExpectCollection(this.response, !this._isNot);
10
+ return new ApiExpectCollection(this.response, !this.isNot);
11
+ }
12
+ /**
13
+ * Tests if Collection have number of items in payload
14
+ * @param min Minimum number of items. Default 1
15
+ * @param max Maximum number of items
16
+ */
17
+ toReturnItems(min, max) {
18
+ let msg = '';
19
+ try {
20
+ msg += `The length of payload array do not match. `;
21
+ const l = this.response.body.payload.length;
22
+ this._expect(l).toBeGreaterThanOrEqual(min || 1);
23
+ if (max)
24
+ this._expect(l).toBeLessThanOrEqual(max);
25
+ }
26
+ catch (e) {
27
+ if (msg)
28
+ e.message = msg + '\n\n' + e.message;
29
+ Error.captureStackTrace(e, this.toReturnItems);
30
+ throw e;
31
+ }
32
+ return this;
15
33
  }
16
- forEach(callbackfn) {
17
- this.response.body.forEach(callbackfn);
34
+ toContainTotalMatches(min, max) {
35
+ let msg = '';
36
+ try {
37
+ msg += `The value of "totalMatches" do not match. `;
38
+ const l = this.response.body.totalMatches;
39
+ this._expect(l).toBeGreaterThanOrEqual(min || 1);
40
+ if (max)
41
+ this._expect(l).toBeLessThanOrEqual(max);
42
+ }
43
+ catch (e) {
44
+ if (msg)
45
+ e.message = msg + '\n\n' + e.message;
46
+ Error.captureStackTrace(e, this.toReturnItems);
47
+ throw e;
48
+ }
18
49
  return this;
19
50
  }
51
+ /**
52
+ * Tests if Collection items matches given object
53
+ * @param expected
54
+ */
20
55
  toMatch(expected) {
21
56
  try {
22
- const v = omitBy(expected, isNil);
23
- for (const item of this.response.body.payload) {
24
- this._expect(item).toMatchObject(v);
25
- }
57
+ expected = omitBy(expected, isNil);
58
+ this._expect(this.response.body.payload).toEqual(expect.arrayContaining([expect.objectContaining(expected)]));
26
59
  }
27
60
  catch (e) {
28
61
  Error.captureStackTrace(e, this.toMatch);
@@ -30,46 +63,49 @@ export class ApiExpectCollection {
30
63
  }
31
64
  return this;
32
65
  }
33
- toHaveFields(keys) {
66
+ /**
67
+ * Tests if Collection items has all of provided fields.
68
+ * @param fields
69
+ */
70
+ toContainFields(fields) {
34
71
  try {
72
+ fields = Array.isArray(fields) ? fields : [fields];
35
73
  for (const item of this.response.body.payload) {
36
- this._expect(item).toHaveFields(keys);
74
+ this._expect(Object.keys(item)).toEqual(expect.arrayContaining(fields));
37
75
  }
38
76
  }
39
77
  catch (e) {
40
- Error.captureStackTrace(e, this.toHaveFields);
78
+ Error.captureStackTrace(e, this.toContainFields);
41
79
  throw e;
42
80
  }
43
81
  return this;
44
82
  }
45
- toHaveFieldsOnly(keys) {
83
+ /**
84
+ * Tests if Collection items only contains all of provided fields.
85
+ * @param fields
86
+ */
87
+ toContainAllFields(fields) {
46
88
  try {
89
+ fields = Array.isArray(fields) ? fields : [fields];
47
90
  for (const item of this.response.body.payload) {
48
- this._expect(item).toHaveFieldsOnly(keys);
91
+ this._expect(Object.keys(item)).toEqual(fields);
49
92
  }
50
93
  }
51
94
  catch (e) {
52
- Error.captureStackTrace(e, this.toHaveFieldsOnly);
95
+ Error.captureStackTrace(e, this.toContainAllFields);
53
96
  throw e;
54
97
  }
55
98
  return this;
56
99
  }
57
- //
58
- // toHaveProperty(keyPath, value?): this {
59
- // try {
60
- // for (const item of this.response.payload) {
61
- // this._expect(item).toHaveProperty(keyPath, value);
62
- // }
63
- //
64
- // } catch (e: any) {
65
- // Error.captureStackTrace(e, this.toHaveProperty);
66
- // throw e;
67
- // }
68
- // return this;
69
- // }
70
- toBeSortedBy(...fields) {
100
+ /**
101
+ * Tests if Collection is sorted by given field(s).
102
+ * @param fields
103
+ */
104
+ toBeSortedBy(fields) {
71
105
  try {
72
- this._expect(this.response.body.payload).toBeSortedBy(fields);
106
+ fields = Array.isArray(fields) ? fields : [fields];
107
+ this._expect(this.response.body.payload)
108
+ .opraCollectionToBeSortedBy(fields);
73
109
  }
74
110
  catch (e) {
75
111
  Error.captureStackTrace(e, this.toBeSortedBy);
@@ -77,6 +113,10 @@ export class ApiExpectCollection {
77
113
  }
78
114
  return this;
79
115
  }
116
+ /**
117
+ * Tests if Collection is filtered by given condition.
118
+ * @param filter
119
+ */
80
120
  toBeFilteredBy(filter) {
81
121
  const f = convertFilter(filter);
82
122
  if (f) {
@@ -92,43 +132,48 @@ export class ApiExpectCollection {
92
132
  }
93
133
  return this;
94
134
  }
95
- toHaveExactItems(expected) {
96
- try {
97
- this._expect(this.response.body.dapayloadta).toHaveLength(expected);
98
- }
99
- catch (e) {
100
- Error.captureStackTrace(e, this.toHaveExactItems);
101
- throw e;
102
- }
103
- return this;
104
- }
105
- toHaveMaxItems(expected) {
106
- try {
107
- this._expect(this.response.body.payload.length).toBeLessThanOrEqual(expected);
108
- }
109
- catch (e) {
110
- Error.captureStackTrace(e, this.toHaveMaxItems);
111
- throw e;
112
- }
113
- return this;
114
- }
115
- toHaveMinItems(expected) {
116
- try {
117
- this._expect(this.response.body.payload.length).toBeGreaterThanOrEqual(expected);
118
- }
119
- catch (e) {
120
- Error.captureStackTrace(e, this.toHaveMinItems);
121
- throw e;
122
- }
123
- return this;
124
- }
125
- _expect(expected) {
126
- const out = expect(expected);
127
- if (this._isNot)
128
- return out.not;
129
- return out;
130
- }
131
135
  }
136
+ expect.extend({
137
+ opraCollectionToBeSortedBy(received, properties) {
138
+ const fieldsMap = properties.map(x => x.split('.'));
139
+ const getValue = (obj, fieldMap) => {
140
+ let v = obj;
141
+ let i = 0;
142
+ while (v && i < fieldMap.length) {
143
+ v = v[fieldMap[i++]];
144
+ }
145
+ return v;
146
+ };
147
+ let pass = true;
148
+ let message;
149
+ if (pass) {
150
+ const sorted = [...(received || [])];
151
+ sorted.sort((a, b) => {
152
+ for (const sortField of fieldsMap) {
153
+ const l = getValue(a, sortField);
154
+ const r = getValue(b, sortField);
155
+ if (l < r)
156
+ return -1;
157
+ if (l > r)
158
+ return 1;
159
+ }
160
+ return 0;
161
+ });
162
+ try {
163
+ expect(received).toEqual(sorted);
164
+ }
165
+ catch (e) {
166
+ pass = false;
167
+ message = () => 'Items are not sorted as expected';
168
+ }
169
+ }
170
+ return {
171
+ actual: received,
172
+ message,
173
+ pass
174
+ };
175
+ },
176
+ });
132
177
  export function convertFilter(str) {
133
178
  const ast = typeof str === 'string' ? OpraFilter.parse(str) : str;
134
179
  if (!ast)
@@ -1,57 +1,21 @@
1
1
  import '../jest-extend/index.js';
2
- import matcherUtils from 'jest-matcher-utils';
3
- import { objectMatches } from '../utils/object-matches.util.js';
4
- import { ApiExpectObject } from './api-expect-object.js';
5
- export class ApiExpectError extends ApiExpectObject {
6
- constructor(response) {
7
- super(response);
8
- this.response = response;
9
- }
10
- toContainDetail(...matching) {
2
+ import isNil from 'lodash.isnil';
3
+ import omitBy from 'lodash.omitby';
4
+ import { ApiExpectBase } from './api-expect-base.js';
5
+ export class ApiExpectError extends ApiExpectBase {
6
+ toMatch(expected) {
11
7
  try {
12
- expect(this.response.body).toBeDefined();
13
- expect(this.response.body.issues).toBeDefined();
14
- expect(this.response.body.issues).apiErrorDetailToContain(matching);
8
+ if (typeof expected === 'string')
9
+ expected = { message: expected };
10
+ else if (expected instanceof RegExp)
11
+ expected = { message: expect.stringMatching(expected) };
12
+ expected = omitBy(expected, isNil);
13
+ this._expect(this.response.body.errors).toEqual(expect.arrayContaining([expect.objectContaining(expected)]));
15
14
  }
16
15
  catch (e) {
17
- Error.captureStackTrace(e, this.toContainDetail);
16
+ Error.captureStackTrace(e, this.toMatch);
18
17
  throw e;
19
18
  }
19
+ return this;
20
20
  }
21
21
  }
22
- expect.extend({
23
- apiErrorDetailToContain(received, issues) {
24
- try {
25
- expect(typeof received).toStrictEqual('object');
26
- expect(Array.isArray(received)).toStrictEqual(true);
27
- }
28
- catch (e) {
29
- return {
30
- pass: false,
31
- message: () => e.message
32
- };
33
- }
34
- for (const m of issues) {
35
- let matched = false;
36
- for (const detail of received) {
37
- if (typeof m === 'object') {
38
- try {
39
- objectMatches(detail, m);
40
- matched = true;
41
- break;
42
- }
43
- catch {
44
- //
45
- }
46
- }
47
- }
48
- if (!matched) {
49
- return {
50
- pass: false,
51
- message: () => `Object does not match: \n` + matcherUtils.stringify(m)
52
- };
53
- }
54
- }
55
- return { actual: true, pass: true, message: () => '' };
56
- },
57
- });
@@ -1,18 +1,18 @@
1
- import '../jest-extend/index.js';
2
1
  import isNil from 'lodash.isnil';
3
2
  import omitBy from 'lodash.omitby';
4
- export class ApiExpectObject {
5
- constructor(response, _isNot = false) {
6
- this.response = response;
7
- this._isNot = _isNot;
8
- }
3
+ import { ApiExpectBase } from './api-expect-base.js';
4
+ export class ApiExpectObject extends ApiExpectBase {
9
5
  get not() {
10
- return new ApiExpectObject(this.response, !this._isNot);
6
+ return new ApiExpectObject(this.response, !this.isNot);
11
7
  }
8
+ /**
9
+ * Tests if Response payload matches given object
10
+ * @param expected
11
+ */
12
12
  toMatch(expected) {
13
13
  try {
14
- const v = omitBy(expected, isNil);
15
- this._expect(this.response.body.payload).toMatchObject(v);
14
+ expected = omitBy(expected, isNil);
15
+ this._expect(this.response.body.payload).toEqual(expect.objectContaining(expected));
16
16
  }
17
17
  catch (e) {
18
18
  Error.captureStackTrace(e, this.toMatch);
@@ -20,30 +20,36 @@ export class ApiExpectObject {
20
20
  }
21
21
  return this;
22
22
  }
23
- toHaveFields(fields) {
23
+ /**
24
+ * Tests if Response payload has all of provided fields.
25
+ * @param fields
26
+ */
27
+ toContainFields(fields) {
24
28
  try {
25
- this._expect(this.response.body.payload).toHaveFields(fields);
29
+ fields = Array.isArray(fields) ? fields : [fields];
30
+ this._expect(Object.keys(this.response.body.payload))
31
+ .toEqual(expect.arrayContaining(fields));
26
32
  }
27
33
  catch (e) {
28
- Error.captureStackTrace(e, this.toHaveFields);
34
+ Error.captureStackTrace(e, this.toContainFields);
29
35
  throw e;
30
36
  }
31
37
  return this;
32
38
  }
33
- toHaveFieldsOnly(fields) {
39
+ /**
40
+ * Tests if Response payload only contains all of provided fields.
41
+ * @param fields
42
+ */
43
+ toContainAllFields(fields) {
34
44
  try {
35
- this._expect(this.response.body.payload).toHaveFieldsOnly(fields);
45
+ fields = Array.isArray(fields) ? fields : [fields];
46
+ this._expect(Object.keys(this.response.body.payload))
47
+ .toEqual(fields);
36
48
  }
37
49
  catch (e) {
38
- Error.captureStackTrace(e, this.toHaveFieldsOnly);
50
+ Error.captureStackTrace(e, this.toContainAllFields);
39
51
  throw e;
40
52
  }
41
53
  return this;
42
54
  }
43
- _expect(expected) {
44
- const out = expect(expected);
45
- if (this._isNot)
46
- return out.not;
47
- return out;
48
- }
49
55
  }
@@ -1,46 +1,24 @@
1
1
  import '../jest-extend/index.js';
2
- export class ApiExpectOperationResult {
3
- constructor(response, _isNot = false) {
4
- this.response = response;
5
- this._isNot = _isNot;
6
- }
2
+ import { ApiExpectBase } from './api-expect-base.js';
3
+ export class ApiExpectOperationResult extends ApiExpectBase {
7
4
  get not() {
8
- return new ApiExpectOperationResult(this.response, !this._isNot);
9
- }
10
- toBeAffectedExact(expected) {
11
- try {
12
- this._expect(this.response.body.affected).toStrictEqual(expected);
13
- }
14
- catch (e) {
15
- Error.captureStackTrace(e, this.toBeAffectedExact);
16
- throw e;
17
- }
18
- return this;
5
+ return new ApiExpectOperationResult(this.response, !this.isNot);
19
6
  }
20
- toBeAffectedMin(expected) {
7
+ toBeAffected(min, max) {
8
+ let msg = '';
21
9
  try {
22
- this._expect(this.response.body.affected).toBeGreaterThanOrEqual(expected);
10
+ msg += `The value of "affected" do not match. `;
11
+ const l = this.response.body.affected;
12
+ this._expect(l).toBeGreaterThanOrEqual(min || 1);
13
+ if (max)
14
+ this._expect(l).toBeLessThanOrEqual(max);
23
15
  }
24
16
  catch (e) {
25
- Error.captureStackTrace(e, this.toBeAffectedMin);
17
+ if (msg)
18
+ e.message = msg + '\n\n' + e.message;
19
+ Error.captureStackTrace(e, this.toBeAffected);
26
20
  throw e;
27
21
  }
28
22
  return this;
29
23
  }
30
- toBeAffectedMax(expected) {
31
- try {
32
- this._expect(this.response.body.affected).toBeLessThanOrEqual(expected);
33
- }
34
- catch (e) {
35
- Error.captureStackTrace(e, this.toBeAffectedMax);
36
- throw e;
37
- }
38
- return this;
39
- }
40
- _expect(expected) {
41
- const out = expect(expected);
42
- if (this._isNot)
43
- return out.not;
44
- return out;
45
- }
46
24
  }