@opra/testing 0.1.1 → 0.2.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.
Files changed (63) hide show
  1. package/cjs/api-response.js +22 -0
  2. package/cjs/expect/api-expect-array.js +188 -0
  3. package/cjs/expect/api-expect-error.js +62 -0
  4. package/cjs/expect/api-expect-object.js +62 -17
  5. package/cjs/expect/api-expect-operation-result.js +43 -0
  6. package/cjs/expect/api-expect.js +84 -25
  7. package/cjs/expect/{jest-extend.js → jest-extend/common.extend.js} +58 -19
  8. package/cjs/expect/utils/object-matches.util.js +32 -0
  9. package/cjs/expect/utils/print-errors.util.js +15 -0
  10. package/cjs/index.js +1 -6
  11. package/cjs/testers/base-operation-tester.js +12 -4
  12. package/cjs/testers/entity-delete-many-tester.js +4 -0
  13. package/cjs/testers/entity-delete-tester.js +2 -0
  14. package/cjs/testers/entity-get-tester.js +2 -0
  15. package/cjs/testers/entity-search-tester.js +1 -1
  16. package/cjs/testers/entity-update-many-tester.js +4 -0
  17. package/cjs/testers/entity-update-tester.js +2 -0
  18. package/esm/api-response.d.ts +12 -0
  19. package/esm/api-response.js +18 -0
  20. package/esm/expect/api-expect-array.d.ts +17 -0
  21. package/esm/expect/api-expect-array.js +182 -0
  22. package/esm/expect/api-expect-error.d.ts +14 -0
  23. package/esm/expect/api-expect-error.js +57 -0
  24. package/esm/expect/api-expect-object.d.ts +11 -9
  25. package/esm/expect/api-expect-object.js +61 -17
  26. package/esm/expect/api-expect-operation-result.d.ts +9 -0
  27. package/esm/expect/api-expect-operation-result.js +39 -0
  28. package/esm/expect/api-expect.d.ts +12 -9
  29. package/esm/expect/api-expect.js +84 -25
  30. package/esm/expect/{jest-extend.d.ts → jest-extend/common.extend.d.ts} +7 -6
  31. package/esm/expect/{jest-extend.js → jest-extend/common.extend.js} +58 -19
  32. package/esm/expect/utils/object-matches.util.d.ts +1 -0
  33. package/esm/expect/utils/object-matches.util.js +28 -0
  34. package/esm/expect/utils/print-errors.util.d.ts +1 -0
  35. package/esm/expect/utils/print-errors.util.js +11 -0
  36. package/esm/index.d.ts +0 -3
  37. package/esm/index.js +0 -4
  38. package/esm/testers/base-operation-tester.d.ts +3 -7
  39. package/esm/testers/base-operation-tester.js +12 -4
  40. package/esm/testers/entity-create-tester.d.ts +2 -2
  41. package/esm/testers/entity-delete-many-tester.d.ts +4 -2
  42. package/esm/testers/entity-delete-many-tester.js +4 -0
  43. package/esm/testers/entity-delete-tester.d.ts +2 -2
  44. package/esm/testers/entity-delete-tester.js +2 -0
  45. package/esm/testers/entity-get-tester.d.ts +2 -2
  46. package/esm/testers/entity-get-tester.js +2 -0
  47. package/esm/testers/entity-search-tester.d.ts +3 -3
  48. package/esm/testers/entity-search-tester.js +1 -1
  49. package/esm/testers/entity-tester.d.ts +8 -8
  50. package/esm/testers/entity-update-many-tester.d.ts +4 -2
  51. package/esm/testers/entity-update-many-tester.js +4 -0
  52. package/esm/testers/entity-update-tester.d.ts +2 -2
  53. package/esm/testers/entity-update-tester.js +2 -0
  54. package/package.json +3 -3
  55. package/cjs/expect/api-expect-body.js +0 -37
  56. package/cjs/expect/api-expect-fail.js +0 -15
  57. package/cjs/expect/api-expect-list.js +0 -130
  58. package/esm/expect/api-expect-body.d.ts +0 -6
  59. package/esm/expect/api-expect-body.js +0 -32
  60. package/esm/expect/api-expect-fail.d.ts +0 -6
  61. package/esm/expect/api-expect-fail.js +0 -11
  62. package/esm/expect/api-expect-list.d.ts +0 -17
  63. package/esm/expect/api-expect-list.js +0 -124
@@ -1,15 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ApiExpectFail = void 0;
4
- const api_expect_body_js_1 = require("./api-expect-body.js");
5
- class ApiExpectFail extends api_expect_body_js_1.ApiExpectBody {
6
- _errors;
7
- constructor(_errors) {
8
- super();
9
- this._errors = _errors;
10
- }
11
- toContain(...issue) {
12
- expect(this._errors).toEqual(expect.arrayContaining(issue.map(o => expect.objectContaining(o))));
13
- }
14
- }
15
- exports.ApiExpectFail = ApiExpectFail;
@@ -1,130 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.convertFilter = exports.ApiExpectList = void 0;
4
- const tslib_1 = require("tslib");
5
- const rule_judgment_1 = tslib_1.__importDefault(require("rule-judgment"));
6
- const url_1 = require("@opra/url");
7
- const api_expect_body_js_1 = require("./api-expect-body.js");
8
- class ApiExpectList extends api_expect_body_js_1.ApiExpectBody {
9
- _body;
10
- constructor(_body) {
11
- super();
12
- this._body = _body;
13
- }
14
- get items() {
15
- return this._body.items;
16
- }
17
- toMatch(value) {
18
- return this._toMatchObject(this._body.items || [], value);
19
- }
20
- haveKeysOnly(keys) {
21
- this._haveKeysOnly(this._body.items || [], keys);
22
- return this;
23
- }
24
- haveKeys(keys) {
25
- this._haveKeys(this._body.items || [], keys);
26
- return this;
27
- }
28
- notHaveKeys(keys) {
29
- this._notHaveKeys(this._body.items || [], keys);
30
- return this;
31
- }
32
- toBeSortedBy(...fields) {
33
- const fieldsMap = fields.map(x => x.split('.'));
34
- const getValue = (obj, fieldMap) => {
35
- let v = obj;
36
- let i = 0;
37
- while (v && i < fieldMap.length) {
38
- v = v[fieldMap[i++]];
39
- }
40
- return v;
41
- };
42
- expect(this._body.items).toBeSorted((a, b) => {
43
- for (const sortField of fieldsMap) {
44
- const l = getValue(a, sortField);
45
- const r = getValue(b, sortField);
46
- if (l < r)
47
- return -1;
48
- if (l > r)
49
- return 1;
50
- }
51
- return 0;
52
- });
53
- return this;
54
- }
55
- toBeFilteredBy(filter) {
56
- const f = convertFilter(filter);
57
- if (f) {
58
- const j = (0, rule_judgment_1.default)(f);
59
- const filtered = this._body.items.filter(j);
60
- expect(this._body.items).toStrictEqual(filtered);
61
- }
62
- return this;
63
- }
64
- toHaveExactItems(expected) {
65
- return expect(this._body.items.length).toStrictEqual(expected);
66
- }
67
- toHaveMaxItems(expected) {
68
- return expect(this._body.items.length).toBeLessThanOrEqual(expected);
69
- }
70
- toHaveMinItems(expected) {
71
- return expect(this._body.items.length).toBeGreaterThanOrEqual(expected);
72
- }
73
- }
74
- exports.ApiExpectList = ApiExpectList;
75
- function convertFilter(str) {
76
- const ast = typeof str === 'string' ? (0, url_1.$parse)(str) : str;
77
- if (!ast)
78
- return;
79
- if (ast instanceof url_1.ComparisonExpression) {
80
- const left = convertFilter(ast.left);
81
- const right = convertFilter(ast.right);
82
- switch (ast.op) {
83
- case '=':
84
- return { $eq: { [left]: right } };
85
- case '!=':
86
- return { $ne: { [left]: right } };
87
- case '>':
88
- return { $gt: { [left]: right } };
89
- case '>=':
90
- return { $gte: { [left]: right } };
91
- case '<':
92
- return { $lt: { [left]: right } };
93
- case '<=':
94
- return { $lte: { [left]: right } };
95
- case 'in':
96
- return { $in: { [left]: right } };
97
- case '!in':
98
- return { $nin: { [left]: right } };
99
- default:
100
- throw new Error(`ComparisonExpression operator (${ast.op}) not implemented yet`);
101
- }
102
- }
103
- if (ast instanceof url_1.QualifiedIdentifier) {
104
- return ast.value;
105
- }
106
- if (ast instanceof url_1.NumberLiteral ||
107
- ast instanceof url_1.StringLiteral ||
108
- ast instanceof url_1.BooleanLiteral ||
109
- ast instanceof url_1.NullLiteral ||
110
- ast instanceof url_1.DateLiteral ||
111
- ast instanceof url_1.TimeLiteral) {
112
- return ast.value;
113
- }
114
- if (ast instanceof url_1.ArrayExpression) {
115
- return ast.items.map(convertFilter);
116
- }
117
- if (ast instanceof url_1.LogicalExpression) {
118
- if (ast.op === 'or')
119
- return { $or: ast.items.map(convertFilter) };
120
- return { $and: ast.items.map(convertFilter) };
121
- }
122
- if (ast instanceof url_1.ArrayExpression) {
123
- return ast.items.map(convertFilter);
124
- }
125
- if (ast instanceof url_1.ParenthesesExpression) {
126
- return convertFilter(ast.expression);
127
- }
128
- throw new Error(`${ast.type} is not implemented yet`);
129
- }
130
- exports.convertFilter = convertFilter;
@@ -1,6 +0,0 @@
1
- export declare class ApiExpectBody {
2
- protected _toMatchObject<T extends {}>(actuals: any[], expected: T): this;
3
- protected _haveKeysOnly(actuals: any[], keys: string[]): this;
4
- protected _haveKeys(actuals: any[], keys: string[]): this;
5
- protected _notHaveKeys(actuals: any[], keys: string[]): this;
6
- }
@@ -1,32 +0,0 @@
1
- import _ from 'lodash';
2
- export class ApiExpectBody {
3
- _toMatchObject(actuals, expected) {
4
- const v = _.omitBy(expected, _.isNil);
5
- for (const actual of actuals)
6
- expect(actual).toMatchObject(v);
7
- return this;
8
- }
9
- _haveKeysOnly(actuals, keys) {
10
- for (const actual of actuals)
11
- expect(actual).toEqual(expect.objectHaveKeysOnly(keys));
12
- return this;
13
- }
14
- _haveKeys(actuals, keys) {
15
- const matcher = keys.reduce((a, k) => {
16
- a[k] = expect.anything();
17
- return a;
18
- }, {});
19
- for (const actual of actuals)
20
- expect(actual).toEqual(expect.objectContaining(matcher));
21
- return this;
22
- }
23
- _notHaveKeys(actuals, keys) {
24
- const matcher = keys.reduce((a, k) => {
25
- a[k] = expect.anything();
26
- return a;
27
- }, {});
28
- for (const actual of actuals)
29
- expect(actual).not.toEqual(expect.objectContaining(matcher));
30
- return this;
31
- }
32
- }
@@ -1,6 +0,0 @@
1
- import { ApiExpectBody } from './api-expect-body.js';
2
- export declare class ApiExpectFail extends ApiExpectBody {
3
- protected _errors: any;
4
- constructor(_errors: any);
5
- toContain(...issue: any[]): void;
6
- }
@@ -1,11 +0,0 @@
1
- import { ApiExpectBody } from './api-expect-body.js';
2
- export class ApiExpectFail extends ApiExpectBody {
3
- _errors;
4
- constructor(_errors) {
5
- super();
6
- this._errors = _errors;
7
- }
8
- toContain(...issue) {
9
- expect(this._errors).toEqual(expect.arrayContaining(issue.map(o => expect.objectContaining(o))));
10
- }
11
- }
@@ -1,17 +0,0 @@
1
- import { Expression } from '@opra/url';
2
- import { ApiExpectBody } from './api-expect-body.js';
3
- export declare class ApiExpectList extends ApiExpectBody {
4
- protected _body: any;
5
- constructor(_body: any);
6
- get items(): any;
7
- toMatch<T extends {}>(value: T): this;
8
- haveKeysOnly(keys: string[]): this;
9
- haveKeys(keys: string[]): this;
10
- notHaveKeys(keys: string[]): this;
11
- toBeSortedBy(...fields: string[]): this;
12
- toBeFilteredBy(filter: string | Expression): this;
13
- toHaveExactItems(expected: number): void;
14
- toHaveMaxItems(expected: number): void;
15
- toHaveMinItems(expected: number): void;
16
- }
17
- export declare function convertFilter(str: string | Expression | undefined): any;
@@ -1,124 +0,0 @@
1
- import ruleJudgment from 'rule-judgment';
2
- import { $parse, ArrayExpression, BooleanLiteral, ComparisonExpression, DateLiteral, LogicalExpression, NullLiteral, NumberLiteral, ParenthesesExpression, QualifiedIdentifier, StringLiteral, TimeLiteral } from '@opra/url';
3
- import { ApiExpectBody } from './api-expect-body.js';
4
- export class ApiExpectList extends ApiExpectBody {
5
- _body;
6
- constructor(_body) {
7
- super();
8
- this._body = _body;
9
- }
10
- get items() {
11
- return this._body.items;
12
- }
13
- toMatch(value) {
14
- return this._toMatchObject(this._body.items || [], value);
15
- }
16
- haveKeysOnly(keys) {
17
- this._haveKeysOnly(this._body.items || [], keys);
18
- return this;
19
- }
20
- haveKeys(keys) {
21
- this._haveKeys(this._body.items || [], keys);
22
- return this;
23
- }
24
- notHaveKeys(keys) {
25
- this._notHaveKeys(this._body.items || [], keys);
26
- return this;
27
- }
28
- toBeSortedBy(...fields) {
29
- const fieldsMap = fields.map(x => x.split('.'));
30
- const getValue = (obj, fieldMap) => {
31
- let v = obj;
32
- let i = 0;
33
- while (v && i < fieldMap.length) {
34
- v = v[fieldMap[i++]];
35
- }
36
- return v;
37
- };
38
- expect(this._body.items).toBeSorted((a, b) => {
39
- for (const sortField of fieldsMap) {
40
- const l = getValue(a, sortField);
41
- const r = getValue(b, sortField);
42
- if (l < r)
43
- return -1;
44
- if (l > r)
45
- return 1;
46
- }
47
- return 0;
48
- });
49
- return this;
50
- }
51
- toBeFilteredBy(filter) {
52
- const f = convertFilter(filter);
53
- if (f) {
54
- const j = ruleJudgment(f);
55
- const filtered = this._body.items.filter(j);
56
- expect(this._body.items).toStrictEqual(filtered);
57
- }
58
- return this;
59
- }
60
- toHaveExactItems(expected) {
61
- return expect(this._body.items.length).toStrictEqual(expected);
62
- }
63
- toHaveMaxItems(expected) {
64
- return expect(this._body.items.length).toBeLessThanOrEqual(expected);
65
- }
66
- toHaveMinItems(expected) {
67
- return expect(this._body.items.length).toBeGreaterThanOrEqual(expected);
68
- }
69
- }
70
- export function convertFilter(str) {
71
- const ast = typeof str === 'string' ? $parse(str) : str;
72
- if (!ast)
73
- return;
74
- if (ast instanceof ComparisonExpression) {
75
- const left = convertFilter(ast.left);
76
- const right = convertFilter(ast.right);
77
- switch (ast.op) {
78
- case '=':
79
- return { $eq: { [left]: right } };
80
- case '!=':
81
- return { $ne: { [left]: right } };
82
- case '>':
83
- return { $gt: { [left]: right } };
84
- case '>=':
85
- return { $gte: { [left]: right } };
86
- case '<':
87
- return { $lt: { [left]: right } };
88
- case '<=':
89
- return { $lte: { [left]: right } };
90
- case 'in':
91
- return { $in: { [left]: right } };
92
- case '!in':
93
- return { $nin: { [left]: right } };
94
- default:
95
- throw new Error(`ComparisonExpression operator (${ast.op}) not implemented yet`);
96
- }
97
- }
98
- if (ast instanceof QualifiedIdentifier) {
99
- return ast.value;
100
- }
101
- if (ast instanceof NumberLiteral ||
102
- ast instanceof StringLiteral ||
103
- ast instanceof BooleanLiteral ||
104
- ast instanceof NullLiteral ||
105
- ast instanceof DateLiteral ||
106
- ast instanceof TimeLiteral) {
107
- return ast.value;
108
- }
109
- if (ast instanceof ArrayExpression) {
110
- return ast.items.map(convertFilter);
111
- }
112
- if (ast instanceof LogicalExpression) {
113
- if (ast.op === 'or')
114
- return { $or: ast.items.map(convertFilter) };
115
- return { $and: ast.items.map(convertFilter) };
116
- }
117
- if (ast instanceof ArrayExpression) {
118
- return ast.items.map(convertFilter);
119
- }
120
- if (ast instanceof ParenthesesExpression) {
121
- return convertFilter(ast.expression);
122
- }
123
- throw new Error(`${ast.type} is not implemented yet`);
124
- }