Skip to content

Commit 3f556ee

Browse files
committed
Add simple test cases
1 parent 554ade3 commit 3f556ee

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

site/test_helpers/mocks.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@ export const MockWorkspace: Workspace = {
3636
created_at: "",
3737
updated_at: "",
3838
project_id: "project-id",
39+
owner_id: "test-user-id",
3940
}

site/util/array.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { firstOrItem } from "./array"
2+
3+
describe("array", () => {
4+
describe("firstOrItem", () => {
5+
it("returns null if empty array", () => {
6+
expect(firstOrItem([])).toBeNull()
7+
})
8+
9+
it("returns first item if array with more one item", () => {
10+
expect(firstOrItem(["a", "b"])).toEqual("a")
11+
})
12+
13+
it("returns item if single item", () => {
14+
expect(firstOrItem("c")).toEqual("c")
15+
})
16+
})
17+
})

site/util/array.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
/**
2+
* Helper function that, given an array or a single item:
3+
* - If an array with no elements, returns null
4+
* - If an array with 1 or more elements, returns the first element
5+
* - If a single item, returns that item
6+
*/
17
export const firstOrItem = <T>(itemOrItems: T | T[]): T | null => {
28
if (Array.isArray(itemOrItems)) {
39
return itemOrItems.length > 0 ? itemOrItems[0] : null

0 commit comments

Comments
 (0)