library
Prebuilt pebble components: drop-in custom elements, no compiler needed
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runAucun
0Étoiles
0Forks
2Issues
| 1 | // The chart core: the reducer (as lifted from saison) and the axis. |
| 2 | |
| 3 | import { describe, expect, test } from "bun:test"; |
| 4 | import { fraction, niceStep, ticks } from "./scale"; |
| 5 | import { chartReducer, initialChartState } from "./state"; |
| 6 | |
| 7 | describe("chartReducer", () => { |
| 8 | test("steps through the items and stops at the ends", () => { |
| 9 | let state = chartReducer(initialChartState, { type: "step", by: "next", count: 3 }); |
| 10 | expect(state.active).toBe(0); |
| 11 | state = chartReducer(state, { type: "step", by: "last", count: 3 }); |
| 12 | expect(state.active).toBe(2); |
| 13 | expect(chartReducer(state, { type: "step", by: "next", count: 3 }).active).toBe(2); |
| 14 | expect(chartReducer(initialChartState, { type: "step", by: "prev", count: 3 }).active).toBe(2); |
| 15 | }); |
| 16 | |
| 17 | test("activating the same item keeps the same state", () => { |
| 18 | const state = chartReducer(initialChartState, { type: "activate", index: 1 }); |
| 19 | expect(chartReducer(state, { type: "activate", index: 1 })).toBe(state); |
| 20 | }); |
| 21 | |
| 22 | test("an empty chart has nothing to step to", () => { |
| 23 | expect(chartReducer(initialChartState, { type: "step", by: "next", count: 0 })).toBe(initialChartState); |
| 24 | }); |
| 25 | }); |
| 26 | |
| 27 | describe("niceStep", () => { |
| 28 | test("rounds to 1, 2 or 5 times a power of ten", () => { |
| 29 | expect(niceStep(119, 6)).toBe(20); |
| 30 | expect(niceStep(10, 4)).toBe(5); |
| 31 | expect(niceStep(0.9, 4)).toBe(0.5); |
| 32 | }); |
| 33 | |
| 34 | test("an empty span steps by 1", () => { |
| 35 | expect(niceStep(0)).toBe(1); |
| 36 | expect(niceStep(-5)).toBe(1); |
| 37 | }); |
| 38 | }); |
| 39 | |
| 40 | describe("ticks", () => { |
| 41 | test("every multiple of the step, across 0", () => { |
| 42 | expect(ticks(-14, 105, 14)).toEqual([-14, 0, 14, 28, 42, 56, 70, 84, 98]); |
| 43 | }); |
| 44 | |
| 45 | test("does not drift on decimal steps", () => { |
| 46 | expect(ticks(0, 0.5, 0.1)).toEqual([0, 0.1, 0.2, 0.3, 0.4, 0.5]); |
| 47 | }); |
| 48 | |
| 49 | test("nothing for a step that can't advance", () => { |
| 50 | expect(ticks(0, 10, 0)).toEqual([]); |
| 51 | expect(ticks(10, 0, 1)).toEqual([]); |
| 52 | }); |
| 53 | }); |
| 54 | |
| 55 | test("fraction places a value between the ends", () => { |
| 56 | expect(fraction(0, -14, 105)).toBeCloseTo(14 / 119); |
| 57 | expect(fraction(5, 5, 5)).toBe(0); |
| 58 | }); |