Skip to content

Commit 46f9652

Browse files
committed
feat: add additional docs and fix input types
1 parent ca46ca0 commit 46f9652

File tree

6 files changed

+63
-7
lines changed

6 files changed

+63
-7
lines changed

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,47 @@ const result = await runAirtableScript({
118118

119119
You can pass one of `us`, `friendly`, `european`, or `iso`.
120120

121+
### Mocking fetch requests
122+
123+
Airtable scripts can either use `fetch`, or in extensions `remoteFetchAsync` to make HTTP requests. You can mock these requests using the `fetchMock` setting:
124+
125+
```js
126+
const result = await runAirtableScript({
127+
script: myScript,
128+
base: baseFixture,
129+
fetchMock: (url, request) => {
130+
return {
131+
status: 200,
132+
body: JSON.stringify({ message: 'Hello, world!' }),
133+
}
134+
},
135+
})
136+
```
137+
138+
### Mocking user inputs
139+
140+
You can mock any `input` from either an automation input or user interaction using the `mockInput` setting:
141+
142+
```js
143+
const results = await runAirtableScript({
144+
script: `
145+
const text = await input.textAsync('Select a table')
146+
output.inspect(text)
147+
`,
148+
base: randomRecords,
149+
mockInput: {
150+
// @ts-ignore
151+
textAsync: (label) => {
152+
if (label === 'Select a table') {
153+
return 'text123'
154+
}
155+
},
156+
},
157+
})
158+
```
159+
160+
Every [input method for extensions or automations](https://airtable.com/developers/scripting/api/input) are available to be mocked. Check out the [input.test.ts](./test/input.test.ts) file for examples.
161+
121162
### Results
122163

123164
The results from calling `runAirtableScript` are an object with several properties:

src/environment/console-aggregator.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ type ConsoleAggregator = {
1313
/**
1414
* Returns a console object that aggregates all messages logged to it.
1515
* Used to override the global console object in tests.
16+
*
17+
* The _getMessages method is called after a test is run to pass the
18+
* messages to the test runner.
1619
*/
1720
const consoleAggregator = (): ConsoleAggregator => {
1821
const consoleMessages: ConsoleMessage[] = []

src/environment/run-airtable-script.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import {
1111

1212
type DefaultDateLocale = 'friendly' | 'us' | 'european' | 'iso'
1313

14+
type Output = [string, number, boolean] | { key: string; value: string }[]
15+
1416
type RunScriptOptions = {
1517
script: string
1618
base: { base: unknown } | unknown
@@ -24,7 +26,7 @@ type RunScriptOptions = {
2426
}
2527

2628
type RunScriptResult = {
27-
output: unknown[]
29+
output: Output
2830
mutations: Mutation[]
2931
console: ConsoleMessage[]
3032
}
@@ -93,7 +95,7 @@ const runAirtableScript = async ({
9395
)
9496

9597
return {
96-
output: context.__output || [],
98+
output: (context.__output as Output) || [],
9799
mutations: context.__mutations || [],
98100
console: context.console._getMessages(),
99101
}

src/environment/sdk/globals/output.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,18 @@ type ExtensionOutput = {
2020
// @ts-ignore
2121
globalThis.__output = []
2222

23+
/**
24+
* The output object if a script is being used within an automation.
25+
*/
2326
const automationOutput: AutomationOutput = {
2427
set: (key, value) => {
2528
__output.push({ key, value })
2629
},
2730
}
2831

32+
/**
33+
* The output object if a script is being used within an extension.
34+
*/
2935
const extensionOutput: ExtensionOutput = {
3036
/**
3137
* Displays the given text on-screen.
@@ -88,6 +94,7 @@ const extensionOutput: ExtensionOutput = {
8894
},
8995
}
9096

97+
// Use one of the two outputs based on the context (extension or automation)
9198
const output: AutomationOutput | ExtensionOutput = globalThis.__inAutomation
9299
? automationOutput
93100
: extensionOutput

src/environment/sdk/lib/pascal-case.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
2-
* A pascal case utility function.
2+
* A pascal case utility function. Used for schema IDs, which always start with
3+
* a three-letter lower-case prefix like tblTableId or fldFieldId.
34
*/
45
const pascalCase = (str: string): string =>
56
str

test/input.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ describe('Input', () => {
1515
config: () => ({ [key]: 'tbl123' }),
1616
},
1717
})
18-
expect(results.output[0].key).toEqual('config')
19-
expect(results.output[0].value).toEqual(
20-
JSON.stringify({ [key]: 'tbl123' })
21-
)
18+
if (typeof results.output[0] === 'object') {
19+
expect(results.output[0].key).toEqual('config')
20+
expect(results.output[0].value).toEqual(
21+
JSON.stringify({ [key]: 'tbl123' })
22+
)
23+
}
2224
})
2325
})
2426
describe('extension script', () => {

0 commit comments

Comments
 (0)