@baselift/blocks-testing 0.0.1
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.
- package/README.md +177 -0
- package/dist/cjs/error_utils.js +56 -0
- package/dist/cjs/index.js +24 -0
- package/dist/cjs/inject_mock_airtable_interface.js +6 -0
- package/dist/cjs/mock_airtable_interface.js +851 -0
- package/dist/cjs/private_utils.js +78 -0
- package/dist/cjs/test_driver.js +658 -0
- package/dist/cjs/test_mutations.js +37 -0
- package/dist/cjs/vacant_airtable_interface.js +367 -0
- package/dist/types/src/error_utils.d.ts +11 -0
- package/dist/types/src/error_utils.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +4 -0
- package/dist/types/src/index.d.ts.map +1 -0
- package/dist/types/src/inject_mock_airtable_interface.d.ts +2 -0
- package/dist/types/src/inject_mock_airtable_interface.d.ts.map +1 -0
- package/dist/types/src/mock_airtable_interface.d.ts +237 -0
- package/dist/types/src/mock_airtable_interface.d.ts.map +1 -0
- package/dist/types/src/private_utils.d.ts +33 -0
- package/dist/types/src/private_utils.d.ts.map +1 -0
- package/dist/types/src/test_driver.d.ts +450 -0
- package/dist/types/src/test_driver.d.ts.map +1 -0
- package/dist/types/src/test_mutations.d.ts +43 -0
- package/dist/types/src/test_mutations.d.ts.map +1 -0
- package/dist/types/src/vacant_airtable_interface.d.ts +2 -0
- package/dist/types/src/vacant_airtable_interface.d.ts.map +1 -0
- package/dist/types/test/index_compatible.test.d.ts +8 -0
- package/dist/types/test/index_compatible.test.d.ts.map +1 -0
- package/dist/types/test/index_incompatible.test.d.ts +2 -0
- package/dist/types/test/index_incompatible.test.d.ts.map +1 -0
- package/dist/types/test/mock_airtable_interface.test.d.ts +2 -0
- package/dist/types/test/mock_airtable_interface.test.d.ts.map +1 -0
- package/dist/types/test/mutation_types.test.d.ts +2 -0
- package/dist/types/test/mutation_types.test.d.ts.map +1 -0
- package/dist/types/test/package_json.test.d.ts +2 -0
- package/dist/types/test/package_json.test.d.ts.map +1 -0
- package/dist/types/test/test_driver.test.d.ts +2 -0
- package/dist/types/test/test_driver.test.d.ts.map +1 -0
- package/dist/types/test/untestable_bindings.test.d.ts +2 -0
- package/dist/types/test/untestable_bindings.test.d.ts.map +1 -0
- package/package.json +120 -0
- package/types/globals.d.ts +6 -0
package/README.md
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
# Introduction to writing automated tests for Airtable Extensions
|
|
2
|
+
|
|
3
|
+
Support for automated testing is currently in open beta.
|
|
4
|
+
|
|
5
|
+
## Implementation status
|
|
6
|
+
|
|
7
|
+
Not all operations are currently supported. In the course of writing tests, authors may find that
|
|
8
|
+
the Extension under test does not function as intended when induced to perform these operations.
|
|
9
|
+
Authors may request support from the SDK maintainers or implement the functionality they need as
|
|
10
|
+
part of their work (this document includes guidance on extending the testing API in a subsequent
|
|
11
|
+
section).
|
|
12
|
+
|
|
13
|
+
The following table describes the implementation status of each operation.
|
|
14
|
+
|
|
15
|
+
| operation | internal support | testing API |
|
|
16
|
+
| ------------------------- | ------------------ | ------------------ |
|
|
17
|
+
| Table - create | partial [1] |
|
|
18
|
+
| Table - update | partial [1] |
|
|
19
|
+
| Table - destroy | | :heavy_check_mark: |
|
|
20
|
+
| Field - create | partial [1] |
|
|
21
|
+
| Field - update | partial [1] |
|
|
22
|
+
| Field - destroy | | :heavy_check_mark: |
|
|
23
|
+
| View - create | |
|
|
24
|
+
| View - update | |
|
|
25
|
+
| View - destroy | | :heavy_check_mark: |
|
|
26
|
+
| Global Config - update | :heavy_check_mark: | n/a [2] |
|
|
27
|
+
| Cursor Data - update | | :heavy_check_mark: |
|
|
28
|
+
| Session Data - update | |
|
|
29
|
+
| User permissions - update | :heavy_check_mark: | :heavy_check_mark: |
|
|
30
|
+
| click settings button | |
|
|
31
|
+
| toggle full screen | |
|
|
32
|
+
| expand record | | :heavy_check_mark: | |
|
|
33
|
+
| Record - create | partial [3] | n/a [2] |
|
|
34
|
+
| Record - update | :heavy_check_mark: | n/a [2] |
|
|
35
|
+
| Record - destroy | :heavy_check_mark: | n/a [2] |
|
|
36
|
+
|
|
37
|
+
- [1] While the SDK's public API supports these operations, its capabilities are restricted.
|
|
38
|
+
Ideally they would be complemented by a more powerful testing API.
|
|
39
|
+
- [2] These operations can be initiated using the SDK's public API.
|
|
40
|
+
- [3] Unsupported operations: creating records for tables which have not yet been loaded,
|
|
41
|
+
[creating records with data for fields which have not yet been loaded](https://github.com/Hyperbase/blocks-sdk/blob/a77fa4b959f512a041e987ee0bfe3fafb7db8b59/packages/sdk/src/models/mutations.ts#L583),
|
|
42
|
+
and creating records in specific views
|
|
43
|
+
|
|
44
|
+
## Key concepts
|
|
45
|
+
|
|
46
|
+
**Test fixture data** is a representation of the state of an Airtable Base, including descriptions
|
|
47
|
+
of Tables, Views, Fields, and Records. It is written by test authors and in terms of
|
|
48
|
+
JSON-serializable values.
|
|
49
|
+
[The Extension Test Fixtures Airtable Extension](https://airtable.com/marketplace/blk5qI32GYyYb1Rbm/test-fixture-generator)
|
|
50
|
+
allows developers to generate this data from the state of an authentic Base. This Extension is not
|
|
51
|
+
ready for beta yet.
|
|
52
|
+
|
|
53
|
+
**`TestDriver`** is the JavaScript interface for creating and manipulating a simulated Airtable
|
|
54
|
+
Extensions environment. It implements methods for performing operations which are not available in
|
|
55
|
+
the SDK.
|
|
56
|
+
|
|
57
|
+
**`AirtableInterface`** is the layer of the SDK which mediates between the models and the backend.
|
|
58
|
+
In production environments, it operates via asynchronous message passing with the Extension's parent
|
|
59
|
+
web browser frame. In testing environments, no transmission occurs, but the `AirtableInterface`
|
|
60
|
+
behaves as though it is receiving relevant messages in order to simulate production behavior.
|
|
61
|
+
|
|
62
|
+
**Mutations** are instructions which describe changes to the Base. Mutations travel through the
|
|
63
|
+
`AirtableInterface`, which sends them to the backend in production environments only.
|
|
64
|
+
|
|
65
|
+
## Writing tests
|
|
66
|
+
|
|
67
|
+
As of January 2021, Airtable Extensions must be written using the React framework. These
|
|
68
|
+
instructions take the presence of React for granted.
|
|
69
|
+
|
|
70
|
+
Every test file must import TestDriver through the `@airtable/blocks-testing` module to ensure that
|
|
71
|
+
the environment is correctly instrumented for automation.
|
|
72
|
+
|
|
73
|
+
Each test will typically perform the following steps:
|
|
74
|
+
|
|
75
|
+
1. Create a TestDriver instance, providing valid fixture data:
|
|
76
|
+
|
|
77
|
+
```js
|
|
78
|
+
const testDriver = new TestDriver({
|
|
79
|
+
/* fixture data here */
|
|
80
|
+
});
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
2. Create an instance of the Extension under test by rendering the Extension's Component as a child
|
|
84
|
+
of the `TestDriver#Container` Component.
|
|
85
|
+
|
|
86
|
+
```js
|
|
87
|
+
render(
|
|
88
|
+
<testDriver.Container>
|
|
89
|
+
<MyExtension />
|
|
90
|
+
</testDriver.Container>,
|
|
91
|
+
);
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
3. Provide some input to the Extension. This may be in the form of a simulated user interaction
|
|
95
|
+
(e.g. via
|
|
96
|
+
[the `@testing-library/user-event` library](https://www.npmjs.com/package/@testing-library/user-event))
|
|
97
|
+
|
|
98
|
+
```js
|
|
99
|
+
// Simulate a user choosing the "Gruyere" option from the table labed
|
|
100
|
+
// "Cheeses".
|
|
101
|
+
const input = screen.getByLabelText('Cheeses');
|
|
102
|
+
const option = screen.getByText('Gruyere');
|
|
103
|
+
userEvent.selectOptions(input, [option]);
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
...or a simulated backend behavior (e.g. via
|
|
107
|
+
[the Blocks SDK](https://airtable.com/developers/apps) or the `TestDriver` API).
|
|
108
|
+
|
|
109
|
+
```js
|
|
110
|
+
// Invoking `createTableAsync` in a test script simulates the condition
|
|
111
|
+
// where the Airtable backend reports that a table has been created by
|
|
112
|
+
// another viewer of the Base.
|
|
113
|
+
await testDriver.base.createTableAsync('a new table', [
|
|
114
|
+
{name: 'address', type: FieldType.EMAIL},
|
|
115
|
+
]);
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
4. Verify that the Extension responded to the input as expected. For many kinds of interactions,
|
|
119
|
+
the Extension's response will be discernible by some change in the UI.
|
|
120
|
+
[The `@testing-library/react` library](https://www.npmjs.com/package/@testing-library/react) is
|
|
121
|
+
a good choice for inspecting the state of the user interface.
|
|
122
|
+
|
|
123
|
+
```js
|
|
124
|
+
// Ensure that the UI updated to display the checkbox as "checked"
|
|
125
|
+
const checkbox = screen.getByRole('checkbox');
|
|
126
|
+
expect(checkbox.checked).toBe(true);
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
...while the `TestDriver` API can be used to verify Extension behaviors which do not influence
|
|
130
|
+
the UI:
|
|
131
|
+
|
|
132
|
+
```js
|
|
133
|
+
// Track every time the Extension attempts to expand a record.
|
|
134
|
+
const recordIds = [];
|
|
135
|
+
testDriver.watch('expandRecord', ({recordId}) => recordIds.push(recordId));
|
|
136
|
+
|
|
137
|
+
// (The code necessary to simulate user inteaction elided from this example.)
|
|
138
|
+
|
|
139
|
+
// Ensure that the Extension attempted to expand the Record with ID `reca`
|
|
140
|
+
expect(recordIds).toEqual(['reca']);
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
[The automated test suite for the To-do List example Extension](https://github.com/Airtable/apps-todo-list/tree/master/test)
|
|
144
|
+
demonstrates this pattern.
|
|
145
|
+
|
|
146
|
+
## Extending the Testing API
|
|
147
|
+
|
|
148
|
+
In order to provide predictable behavior, the Airtable testing platform relies on simulations of
|
|
149
|
+
Base operations. Every simulation requires some amount of support in the testing platform. Some
|
|
150
|
+
simulations also require a dedicated testing API.
|
|
151
|
+
|
|
152
|
+
**Implementing internal support** Every Base operation requires some amount of simulation code in
|
|
153
|
+
order to support scripting in test environments.
|
|
154
|
+
|
|
155
|
+
For some operations, this simulation may be as limited as a "no-op" implementation of an internal
|
|
156
|
+
method. These operations include mutations which the SDK applies optimistically and messages with no
|
|
157
|
+
direct effect on the Extension.
|
|
158
|
+
|
|
159
|
+
Other operations will require more advanced simulation logic. Operations of this type generally rely
|
|
160
|
+
on the backend's response to determine their effect on the Base. The simulated behavior should be
|
|
161
|
+
predictable, mimicking the expected response in production.
|
|
162
|
+
|
|
163
|
+
**Implementing a dedicated API** A dedicated testing API is not necessary for any operation which
|
|
164
|
+
can be expressed through the public API of the SDK (e.g. "create many records" or "delete a table").
|
|
165
|
+
When a test author wishes to script such an operation, they should use the SDK directly. This is
|
|
166
|
+
valid because an Extension cannot distinguish between operations initiated by the backend and
|
|
167
|
+
operations initiated by a test script using the SDK.
|
|
168
|
+
|
|
169
|
+
Many other operations cannot be expressed via the public API of the SDK. The `TestDriver` API must
|
|
170
|
+
be extended to allow test authors to simulate these operations.
|
|
171
|
+
|
|
172
|
+
Some of these restrictions are circumstantial, owing to the fact that as of January 2021, the SDK is
|
|
173
|
+
incomplete and under active development. This includes an API to delete a Field. Other restrictions
|
|
174
|
+
are fundamental to the design of the Extensions platform. For instance, the SDK intentionally omits
|
|
175
|
+
a method for an Extension to change the permissions of the current user. This distinction may inform
|
|
176
|
+
the decision to introduce a testing API, since doing so will increase maintenance responsibilities
|
|
177
|
+
in a way that may be obviated by future extensions to the SDK's public API.
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.invariant = invariant;
|
|
7
|
+
exports.spawnError = spawnError;
|
|
8
|
+
require("core-js/modules/es.regexp.exec.js");
|
|
9
|
+
require("core-js/modules/es.string.replace.js");
|
|
10
|
+
/**
|
|
11
|
+
* @internal
|
|
12
|
+
*/
|
|
13
|
+
function spawnErrorWithOriginOmittedFromStackTrace(errorMessageFormat, errorMessageArgs, errorOriginFn) {
|
|
14
|
+
var safeMessage = errorMessageFormat;
|
|
15
|
+
var argIndex = 0;
|
|
16
|
+
var formattedMessage = errorMessageFormat.replace(/%s/g, () => {
|
|
17
|
+
var arg = errorMessageArgs ? errorMessageArgs[argIndex] : undefined;
|
|
18
|
+
argIndex++;
|
|
19
|
+
return String(arg);
|
|
20
|
+
});
|
|
21
|
+
var err = new Error(formattedMessage);
|
|
22
|
+
if (Error.captureStackTrace && errorOriginFn) {
|
|
23
|
+
Error.captureStackTrace(err, errorOriginFn);
|
|
24
|
+
}
|
|
25
|
+
Object.defineProperty(err, '__safeMessage', {
|
|
26
|
+
configurable: false,
|
|
27
|
+
enumerable: false,
|
|
28
|
+
value: safeMessage,
|
|
29
|
+
writable: false
|
|
30
|
+
});
|
|
31
|
+
return err;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @hidden
|
|
36
|
+
*/
|
|
37
|
+
function spawnError(errorMessageFormat) {
|
|
38
|
+
for (var _len = arguments.length, errorMessageArgs = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
|
|
39
|
+
errorMessageArgs[_key - 1] = arguments[_key];
|
|
40
|
+
}
|
|
41
|
+
return spawnErrorWithOriginOmittedFromStackTrace(errorMessageFormat, errorMessageArgs, spawnError);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* An alternative to facebook's invariant that's safe to use with base data
|
|
46
|
+
*
|
|
47
|
+
* @hidden
|
|
48
|
+
*/
|
|
49
|
+
function invariant(condition, errorMessageFormat) {
|
|
50
|
+
if (!condition) {
|
|
51
|
+
for (var _len2 = arguments.length, errorMessageArgs = new Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) {
|
|
52
|
+
errorMessageArgs[_key2 - 2] = arguments[_key2];
|
|
53
|
+
}
|
|
54
|
+
throw spawnErrorWithOriginOmittedFromStackTrace(errorMessageFormat, errorMessageArgs, invariant);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
Object.defineProperty(exports, "__esModule", {
|
|
5
|
+
value: true
|
|
6
|
+
});
|
|
7
|
+
Object.defineProperty(exports, "MutationTypes", {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
get: function get() {
|
|
10
|
+
return _unstable_testing_utils.MutationTypes;
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
Object.defineProperty(exports, "default", {
|
|
14
|
+
enumerable: true,
|
|
15
|
+
get: function get() {
|
|
16
|
+
return _test_driver.default;
|
|
17
|
+
}
|
|
18
|
+
});
|
|
19
|
+
var _unstable_testing_utils = require("@baselift/blocks/unstable_testing_utils");
|
|
20
|
+
var _semver = _interopRequireDefault(require("semver"));
|
|
21
|
+
require("./inject_mock_airtable_interface");
|
|
22
|
+
var _error_utils = require("./error_utils");
|
|
23
|
+
var _test_driver = _interopRequireDefault(require("./test_driver"));
|
|
24
|
+
(0, _error_utils.invariant)(_semver.default.satisfies(_unstable_testing_utils.Sdk.VERSION, "^1.2.2"), 'Version %s of the blocks-testing library does not support version %s of the Blocks SDK library.', "0.0.1", _unstable_testing_utils.Sdk.VERSION);
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
|
+
var _vacant_airtable_interface = _interopRequireDefault(require("./vacant_airtable_interface"));
|
|
5
|
+
var vacantAirtableInterface = new _vacant_airtable_interface.default();
|
|
6
|
+
window.__getAirtableInterfaceAtVersion = () => vacantAirtableInterface;
|