-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Making typed function handlers easier (#33)
* adding types for Slack Function handlers * adding tests and handling cases with no input or output params * adding tasks * clarify things a little * adding better assertions on function handler type tests
- Loading branch information
1 parent
05d87a5
commit c13d017
Showing
10 changed files
with
448 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.DS_Store | ||
.coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"tasks": { | ||
"test": "deno test src && deno fmt --check src && deno lint src", | ||
"coverage": "deno test --allow-read --coverage=.coverage && deno coverage --exclude=fixtures|test .coverage" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
export { | ||
assertEquals, | ||
assertExists, | ||
assertStrictEquals, | ||
} from "https://deno.land/[email protected]/testing/asserts.ts"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
import { assertEquals } from "../dev_deps.ts"; | ||
import { SlackFunctionTester } from "./function_tester.ts"; | ||
import { BaseSlackFunctionHandler } from "./types.ts"; | ||
|
||
// These tests are to ensure our Function Handler types are supporting the use cases we want to | ||
// Any "failures" here will most likely be reflected in Type errors | ||
|
||
Deno.test("BaseSlackFunctionHandler types", () => { | ||
type Inputs = { | ||
in: string; | ||
}; | ||
type Outputs = { | ||
out: string; | ||
}; | ||
const handler: BaseSlackFunctionHandler<Inputs, Outputs> = ( | ||
{ inputs }, | ||
) => { | ||
return { | ||
outputs: { | ||
out: inputs.in, | ||
}, | ||
}; | ||
}; | ||
const { createContext } = SlackFunctionTester("test"); | ||
const inputs = { in: "test" }; | ||
const result = handler(createContext({ inputs })); | ||
assertEquals(result.outputs?.out, inputs.in); | ||
}); | ||
|
||
Deno.test("BaseSlackFunctionHandler with empty inputs and outputs", () => { | ||
type Inputs = Record<never, never>; | ||
type Outputs = Record<never, never>; | ||
const handler: BaseSlackFunctionHandler<Inputs, Outputs> = () => { | ||
return { | ||
outputs: {}, | ||
}; | ||
}; | ||
const { createContext } = SlackFunctionTester("test"); | ||
const result = handler(createContext({ inputs: {} })); | ||
assertEquals(result.outputs, {}); | ||
}); | ||
|
||
Deno.test("BaseSlackFunctionHandler with undefined inputs and outputs", () => { | ||
type Inputs = undefined; | ||
type Outputs = undefined; | ||
const handler: BaseSlackFunctionHandler<Inputs, Outputs> = () => { | ||
return { | ||
outputs: {}, | ||
}; | ||
}; | ||
const { createContext } = SlackFunctionTester("test"); | ||
const result = handler(createContext({ inputs: undefined })); | ||
assertEquals(result.outputs, {}); | ||
}); | ||
|
||
Deno.test("BaseSlackFunctionHandler with inputs and empty outputs", () => { | ||
type Inputs = { | ||
in: string; | ||
}; | ||
type Outputs = Record<never, never>; | ||
const handler: BaseSlackFunctionHandler<Inputs, Outputs> = ({ inputs }) => { | ||
const _test = inputs.in; | ||
|
||
return { | ||
outputs: {}, | ||
}; | ||
}; | ||
const { createContext } = SlackFunctionTester("test"); | ||
const inputs = { in: "test" }; | ||
const result = handler(createContext({ inputs })); | ||
assertEquals(result.outputs, {}); | ||
}); | ||
|
||
Deno.test("BaseSlackFunctionHandler with empty inputs and outputs", () => { | ||
type Inputs = Record<never, never>; | ||
type Outputs = { | ||
out: string; | ||
}; | ||
const handler: BaseSlackFunctionHandler<Inputs, Outputs> = () => { | ||
return { | ||
outputs: { | ||
out: "test", | ||
}, | ||
}; | ||
}; | ||
const { createContext } = SlackFunctionTester("test"); | ||
const result = handler(createContext({ inputs: {} })); | ||
assertEquals(result.outputs?.out, "test"); | ||
}); | ||
|
||
Deno.test("BaseSlackFunctionHandler with any inputs and any outputs", () => { | ||
// deno-lint-ignore no-explicit-any | ||
const handler: BaseSlackFunctionHandler<any, any> = ({ inputs }) => { | ||
return { | ||
outputs: { | ||
out: inputs.in, | ||
}, | ||
}; | ||
}; | ||
const { createContext } = SlackFunctionTester("test"); | ||
const inputs = { in: "test" }; | ||
const result = handler(createContext({ inputs })); | ||
assertEquals(result.outputs?.out, inputs.in); | ||
}); | ||
|
||
Deno.test("BaseSlackFunctionHandler with set inputs and any outputs", () => { | ||
type Inputs = { | ||
in: string; | ||
}; | ||
// deno-lint-ignore no-explicit-any | ||
const handler: BaseSlackFunctionHandler<Inputs, any> = ({ inputs }) => { | ||
return { | ||
outputs: { | ||
out: inputs.in, | ||
}, | ||
}; | ||
}; | ||
const { createContext } = SlackFunctionTester("test"); | ||
const inputs = { in: "test" }; | ||
const result = handler(createContext({ inputs })); | ||
assertEquals(result.outputs?.out, inputs.in); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,188 @@ | ||
import { assertEquals } from "../dev_deps.ts"; | ||
import { SlackFunctionTester } from "./function_tester.ts"; | ||
import { DefineFunction } from "./mod.ts"; | ||
import { SlackFunctionHandler } from "./types.ts"; | ||
|
||
// These tests are to ensure our Function Handler types are supporting the use cases we want to | ||
// Any "failures" here will most likely be reflected in Type errors | ||
|
||
Deno.test("SlackFunctionHandler with inputs and outputs", () => { | ||
const TestFn = DefineFunction({ | ||
callback_id: "test", | ||
title: "test fn", | ||
source_file: "test.ts", | ||
input_parameters: { | ||
properties: { | ||
in: { | ||
type: "string", | ||
}, | ||
}, | ||
required: ["in"], | ||
}, | ||
output_parameters: { | ||
properties: { | ||
out: { | ||
type: "string", | ||
}, | ||
}, | ||
required: ["out"], | ||
}, | ||
}); | ||
const handler: SlackFunctionHandler<typeof TestFn.definition> = ( | ||
{ inputs }, | ||
) => { | ||
return { | ||
outputs: { | ||
out: inputs.in, | ||
}, | ||
}; | ||
}; | ||
const { createContext } = SlackFunctionTester("test"); | ||
const inputs = { in: "test" }; | ||
const result = handler(createContext({ inputs })); | ||
assertEquals(result.outputs?.out, inputs.in); | ||
}); | ||
|
||
Deno.test("SlackFunctionHandler with optional input", () => { | ||
const TestFn = DefineFunction({ | ||
callback_id: "test", | ||
title: "test fn", | ||
source_file: "test.ts", | ||
input_parameters: { | ||
properties: { | ||
in: { | ||
type: "string", | ||
}, | ||
}, | ||
required: [], | ||
}, | ||
output_parameters: { | ||
properties: { | ||
out: { | ||
type: "string", | ||
}, | ||
}, | ||
required: ["out"], | ||
}, | ||
}); | ||
const handler: SlackFunctionHandler<typeof TestFn.definition> = ( | ||
{ inputs }, | ||
) => { | ||
return { | ||
outputs: { | ||
out: inputs.in || "default", | ||
}, | ||
}; | ||
}; | ||
const { createContext } = SlackFunctionTester("test"); | ||
const inputs = {}; | ||
const result = handler(createContext({ inputs })); | ||
assertEquals(result.outputs?.out, "default"); | ||
}); | ||
|
||
Deno.test("SlackFunctionHandler with no inputs or outputs", () => { | ||
const TestFn = DefineFunction({ | ||
callback_id: "test", | ||
title: "test fn", | ||
source_file: "test.ts", | ||
}); | ||
const handler: SlackFunctionHandler<typeof TestFn.definition> = () => { | ||
return { | ||
outputs: {}, | ||
}; | ||
}; | ||
const { createContext } = SlackFunctionTester("test"); | ||
const result = handler(createContext({ inputs: {} })); | ||
assertEquals(result.outputs, {}); | ||
}); | ||
|
||
Deno.test("SlackFunctionHandler with undefined inputs and outputs", () => { | ||
const TestFn = DefineFunction({ | ||
callback_id: "test", | ||
title: "test fn", | ||
source_file: "test.ts", | ||
input_parameters: undefined, | ||
output_parameters: undefined, | ||
}); | ||
const handler: SlackFunctionHandler<typeof TestFn.definition> = () => { | ||
return { | ||
outputs: {}, | ||
}; | ||
}; | ||
const { createContext } = SlackFunctionTester("test"); | ||
const result = handler(createContext({ inputs: {} })); | ||
assertEquals(result.outputs, {}); | ||
}); | ||
|
||
Deno.test("SlackFunctionHandler with empty inputs and outputs", () => { | ||
const TestFn = DefineFunction({ | ||
callback_id: "test", | ||
title: "test fn", | ||
source_file: "test.ts", | ||
input_parameters: { properties: {}, required: [] }, | ||
output_parameters: { properties: {}, required: [] }, | ||
}); | ||
const handler: SlackFunctionHandler<typeof TestFn.definition> = () => { | ||
return { | ||
outputs: {}, | ||
}; | ||
}; | ||
const { createContext } = SlackFunctionTester("test"); | ||
const result = handler(createContext({ inputs: {} })); | ||
assertEquals(result.outputs, {}); | ||
}); | ||
|
||
Deno.test("SlackFunctionHandler with only inputs", () => { | ||
const TestFn = DefineFunction({ | ||
callback_id: "test", | ||
title: "test fn", | ||
source_file: "test.ts", | ||
input_parameters: { | ||
properties: { | ||
in: { | ||
type: "string", | ||
}, | ||
}, | ||
required: ["in"], | ||
}, | ||
}); | ||
const handler: SlackFunctionHandler<typeof TestFn.definition> = ( | ||
{ inputs }, | ||
) => { | ||
const _test = inputs.in; | ||
|
||
return { | ||
outputs: {}, | ||
}; | ||
}; | ||
const { createContext } = SlackFunctionTester("test"); | ||
const inputs = { in: "test" }; | ||
const result = handler(createContext({ inputs })); | ||
assertEquals(result.outputs, {}); | ||
}); | ||
|
||
Deno.test("SlackFunctionHandler with only outputs", () => { | ||
const TestFn = DefineFunction({ | ||
callback_id: "test", | ||
title: "test fn", | ||
source_file: "test.ts", | ||
output_parameters: { | ||
properties: { | ||
out: { | ||
type: "string", | ||
}, | ||
}, | ||
required: ["out"], | ||
}, | ||
}); | ||
const handler: SlackFunctionHandler<typeof TestFn.definition> = () => { | ||
return { | ||
outputs: { | ||
out: "test", | ||
}, | ||
}; | ||
}; | ||
const { createContext } = SlackFunctionTester("test"); | ||
const result = handler(createContext({ inputs: {} })); | ||
assertEquals(result.outputs?.out, "test"); | ||
}); |
Oops, something went wrong.