|
1 | 1 | import { isString, pipe, when } from "../src"; |
2 | 2 |
|
3 | 3 | describe("when", function () { |
4 | | - const test = <T>(value: T) => |
5 | | - when( |
6 | | - (value) => value === 100, |
7 | | - (value) => { |
8 | | - expect(value).toBeTruthy(); |
9 | | - }, |
10 | | - value, |
11 | | - ); |
12 | | - const withPipe = <T>(value: T) => |
13 | | - pipe( |
14 | | - value, |
15 | | - test, |
16 | | - when(isString, () => "Hello fxts"), |
17 | | - ); |
18 | | - |
19 | | - it("If the input value is a number, it will be filtered out by the first 'when' function. And throw undefined.", () => { |
20 | | - const INPUT_VALUE = 100; |
21 | | - |
22 | | - test(INPUT_VALUE); |
23 | | - withPipe(INPUT_VALUE); |
24 | | - }); |
25 | | - it("If the input value is a string, it will be filtered out by the second 'when' function. And return value is 'Hello fxts'", () => { |
26 | | - const INPUT_VALUE = "Hello World"; |
| 4 | + describe("with a general predicate", () => { |
| 5 | + const test = <T>(value: T) => |
| 6 | + when( |
| 7 | + (value) => value === 100, |
| 8 | + (value) => { |
| 9 | + expect(value).toBeTruthy(); |
| 10 | + return "value is 100"; |
| 11 | + }, |
| 12 | + value, |
| 13 | + ); |
| 14 | + const withPipe = <T>(value: T) => |
| 15 | + pipe( |
| 16 | + value, |
| 17 | + test, |
| 18 | + when(isString, () => "Hello fxts"), |
| 19 | + ); |
| 20 | + |
| 21 | + it("If the input value is a number 100, the callback is executed and returns its result.", () => { |
| 22 | + const INPUT_VALUE = 100; |
| 23 | + expect(test(INPUT_VALUE)).toBe("value is 100"); |
| 24 | + }); |
| 25 | + |
| 26 | + it("If the input value does not match predicate, it returns the original value.", () => { |
| 27 | + const INPUT_VALUE = "Hello World"; |
| 28 | + expect(test(INPUT_VALUE)).toBe("Hello World"); |
| 29 | + }); |
27 | 30 |
|
28 | | - expect(test(INPUT_VALUE)).toBe("Hello World"); |
29 | | - expect(withPipe(INPUT_VALUE)).toBe("Hello fxts"); |
| 31 | + it("If nothing matches, all 'when' functions will be passed.", () => { |
| 32 | + const INPUT_VALUE = [1, 2, 3, 4]; |
| 33 | + expect(test(INPUT_VALUE)).toEqual(INPUT_VALUE); |
| 34 | + expect(withPipe(INPUT_VALUE)).toEqual(INPUT_VALUE); |
| 35 | + }); |
30 | 36 | }); |
31 | | - it("If nothing matches, all 'when' functions will be passed.", () => { |
32 | | - const INPUT_VALUE = [1, 2, 3, 4]; |
33 | 37 |
|
34 | | - expect(test(INPUT_VALUE)).toEqual(INPUT_VALUE); |
35 | | - expect(withPipe(INPUT_VALUE)).toEqual(INPUT_VALUE); |
| 38 | + describe("with a type guard predicate", () => { |
| 39 | + type Shape = |
| 40 | + | { type: "circle"; radius: number } |
| 41 | + | { type: "square"; side: number }; |
| 42 | + |
| 43 | + const isCircle = ( |
| 44 | + shape: Shape, |
| 45 | + ): shape is { type: "circle"; radius: number } => shape.type === "circle"; |
| 46 | + |
| 47 | + it("should return the result of the callback when the type guard is true", () => { |
| 48 | + const myShape: Shape = { type: "circle", radius: 10 }; |
| 49 | + const result = when( |
| 50 | + isCircle, |
| 51 | + (circle) => `A circle with radius ${circle.radius}`, |
| 52 | + myShape, |
| 53 | + ); |
| 54 | + expect(result).toBe("A circle with radius 10"); |
| 55 | + }); |
| 56 | + |
| 57 | + it("should return the original value when the type guard is false", () => { |
| 58 | + const anotherShape: Shape = { type: "square", side: 5 }; |
| 59 | + const result = when( |
| 60 | + isCircle, |
| 61 | + (circle) => `A circle with radius ${circle.radius}`, |
| 62 | + anotherShape, |
| 63 | + ); |
| 64 | + expect(result).toEqual(anotherShape); |
| 65 | + }); |
| 66 | + |
| 67 | + it("should work correctly with pipe", () => { |
| 68 | + const myShape: Shape = { type: "circle", radius: 10 }; |
| 69 | + const anotherShape: Shape = { type: "square", side: 5 }; |
| 70 | + |
| 71 | + const shapeHandler = when( |
| 72 | + isCircle, |
| 73 | + (circle) => `A circle with radius ${circle.radius}`, |
| 74 | + ); |
| 75 | + |
| 76 | + expect(pipe(myShape, shapeHandler)).toBe("A circle with radius 10"); |
| 77 | + expect(pipe(anotherShape, shapeHandler)).toEqual(anotherShape); |
| 78 | + }); |
36 | 79 | }); |
37 | 80 | }); |
0 commit comments