|
| 1 | +// Copyright (C) Parity Technologies (UK) Ltd. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | + |
| 16 | +import { describe, it, expect } from "vitest"; |
| 17 | +import { firstEnabledIndex, nextEnabledIndex } from "./selectNav.js"; |
| 18 | + |
| 19 | +// Mirrors the deploy signer picker: phone disabled (logged out), dev enabled. |
| 20 | +const PHONE_DISABLED = [{ disabled: true }, {}]; |
| 21 | +const ALL_ENABLED = [{}, {}, {}]; |
| 22 | +const MIDDLE_DISABLED = [{}, { disabled: true }, {}]; |
| 23 | +const TAIL_DISABLED = [{}, { disabled: true }, { disabled: true }]; |
| 24 | + |
| 25 | +describe("firstEnabledIndex", () => { |
| 26 | + it("returns the start index when it is enabled", () => { |
| 27 | + expect(firstEnabledIndex(ALL_ENABLED, 0)).toBe(0); |
| 28 | + expect(firstEnabledIndex(ALL_ENABLED, 2)).toBe(2); |
| 29 | + }); |
| 30 | + |
| 31 | + it("skips a disabled start to the next enabled option (deploy logged-out case)", () => { |
| 32 | + // The deploy picker passes initialIndex 0 with phone disabled; the |
| 33 | + // cursor must land on the dev signer at index 1. |
| 34 | + expect(firstEnabledIndex(PHONE_DISABLED, 0)).toBe(1); |
| 35 | + }); |
| 36 | + |
| 37 | + it("wraps past a disabled tail back to an enabled head", () => { |
| 38 | + expect(firstEnabledIndex(TAIL_DISABLED, 1)).toBe(0); |
| 39 | + expect(firstEnabledIndex(TAIL_DISABLED, 2)).toBe(0); |
| 40 | + }); |
| 41 | + |
| 42 | + it("falls back to start when every option is disabled", () => { |
| 43 | + expect(firstEnabledIndex([{ disabled: true }, { disabled: true }], 1)).toBe(1); |
| 44 | + }); |
| 45 | + |
| 46 | + it("returns the only index for a single-option list", () => { |
| 47 | + expect(firstEnabledIndex([{}], 0)).toBe(0); |
| 48 | + expect(firstEnabledIndex([{ disabled: true }], 0)).toBe(0); |
| 49 | + }); |
| 50 | +}); |
| 51 | + |
| 52 | +describe("nextEnabledIndex", () => { |
| 53 | + it("moves to the adjacent enabled option in each direction", () => { |
| 54 | + expect(nextEnabledIndex(ALL_ENABLED, 0, 1)).toBe(1); |
| 55 | + expect(nextEnabledIndex(ALL_ENABLED, 1, -1)).toBe(0); |
| 56 | + }); |
| 57 | + |
| 58 | + it("wraps around the ends", () => { |
| 59 | + expect(nextEnabledIndex(ALL_ENABLED, 2, 1)).toBe(0); |
| 60 | + expect(nextEnabledIndex(ALL_ENABLED, 0, -1)).toBe(2); |
| 61 | + }); |
| 62 | + |
| 63 | + it("skips over a disabled option in both directions", () => { |
| 64 | + // index 1 is disabled, so forward from 0 lands on 2, backward from 2 lands on 0. |
| 65 | + expect(nextEnabledIndex(MIDDLE_DISABLED, 0, 1)).toBe(2); |
| 66 | + expect(nextEnabledIndex(MIDDLE_DISABLED, 2, -1)).toBe(0); |
| 67 | + }); |
| 68 | + |
| 69 | + it("never lands on the disabled phone option (deploy logged-out case)", () => { |
| 70 | + // Starting on dev (index 1), both directions wrap back to dev, never phone (0). |
| 71 | + expect(nextEnabledIndex(PHONE_DISABLED, 1, 1)).toBe(1); |
| 72 | + expect(nextEnabledIndex(PHONE_DISABLED, 1, -1)).toBe(1); |
| 73 | + }); |
| 74 | + |
| 75 | + it("stays put when every other option is disabled", () => { |
| 76 | + expect(nextEnabledIndex(TAIL_DISABLED, 0, 1)).toBe(0); |
| 77 | + expect(nextEnabledIndex(TAIL_DISABLED, 0, -1)).toBe(0); |
| 78 | + }); |
| 79 | + |
| 80 | + it("stays put for a single-option list", () => { |
| 81 | + expect(nextEnabledIndex([{}], 0, 1)).toBe(0); |
| 82 | + expect(nextEnabledIndex([{}], 0, -1)).toBe(0); |
| 83 | + }); |
| 84 | +}); |
0 commit comments