|
| 1 | +describe("hintAnimation logic", () => { |
| 2 | + const dataHintAnimationAttribute = "data-hint-animation"; |
| 3 | + |
| 4 | + let element: HTMLElement; |
| 5 | + let hint: { getOption: (key: string) => boolean }; |
| 6 | + |
| 7 | + beforeEach(() => { |
| 8 | + element = document.createElement("div"); |
| 9 | + |
| 10 | + hint = { |
| 11 | + getOption: jest.fn(() => true), // default: true |
| 12 | + }; |
| 13 | + }); |
| 14 | + |
| 15 | + function getHintAnimation(element: HTMLElement, hint: any): boolean { |
| 16 | + const hintAnimationAttr = element.getAttribute(dataHintAnimationAttribute); |
| 17 | + let hintAnimation: boolean = hint.getOption("hintAnimation"); |
| 18 | + if (hintAnimationAttr) { |
| 19 | + hintAnimation = hintAnimationAttr === "true"; |
| 20 | + } |
| 21 | + return hintAnimation; |
| 22 | + } |
| 23 | + |
| 24 | + test("should use hint.getOption when no attribute is set", () => { |
| 25 | + (hint.getOption as jest.Mock).mockReturnValue(true); |
| 26 | + |
| 27 | + const result = getHintAnimation(element, hint); |
| 28 | + |
| 29 | + expect(result).toBe(true); |
| 30 | + expect(hint.getOption).toHaveBeenCalledWith("hintAnimation"); |
| 31 | + }); |
| 32 | + |
| 33 | + test("should override with true when attribute is 'true'", () => { |
| 34 | + element.setAttribute(dataHintAnimationAttribute, "true"); |
| 35 | + (hint.getOption as jest.Mock).mockReturnValue(false); // default false |
| 36 | + |
| 37 | + const result = getHintAnimation(element, hint); |
| 38 | + |
| 39 | + expect(result).toBe(true); |
| 40 | + }); |
| 41 | + |
| 42 | + test("should override with false when attribute is 'false'", () => { |
| 43 | + element.setAttribute(dataHintAnimationAttribute, "false"); |
| 44 | + (hint.getOption as jest.Mock).mockReturnValue(true); // default true |
| 45 | + |
| 46 | + const result = getHintAnimation(element, hint); |
| 47 | + |
| 48 | + expect(result).toBe(false); |
| 49 | + }); |
| 50 | +}); |
0 commit comments