classnames.spec.js
1 month ago
dateUtils.spec.js
1 month ago
formatters.spec.js
1 month ago
pluginUtils.spec.js
1 month ago
transformers.spec.js
1 month ago
pluginUtils.spec.js
95 lines
| 1 | // Mock the WordPress and Force UI imports so importing pluginUtils doesn't |
| 2 | // require a live REST endpoint or a real toast renderer. We only exercise |
| 3 | // generateSuggestions, which is pure, but the module-level imports still |
| 4 | // resolve at require() time. |
| 5 | jest.mock("@wordpress/api-fetch", () => jest.fn()); |
| 6 | jest.mock("@bsf/force-ui", () => ({ |
| 7 | toast: { success: jest.fn(), info: jest.fn(), error: jest.fn() }, |
| 8 | })); |
| 9 | |
| 10 | import { generateSuggestions } from "../pluginUtils"; |
| 11 | |
| 12 | const makePlugins = (slugs) => |
| 13 | slugs.map((slug) => ({ slug, init: `${slug}/${slug}.php`, name: slug })); |
| 14 | |
| 15 | describe("generateSuggestions", () => { |
| 16 | it("lists non-active primaries first and stamps their status", () => { |
| 17 | const statuses = { p1: "inactive", p2: "not-installed" }; |
| 18 | const out = generateSuggestions( |
| 19 | statuses, |
| 20 | makePlugins(["p1", "p2"]), |
| 21 | [] |
| 22 | ); |
| 23 | expect(out.map((p) => p.slug)).toEqual(["p1", "p2"]); |
| 24 | expect(out[0].status).toBe("inactive"); |
| 25 | expect(out[1].status).toBe("not-installed"); |
| 26 | }); |
| 27 | |
| 28 | it("fills remaining slots with non-active secondaries", () => { |
| 29 | const statuses = { p1: "inactive", s1: "inactive", s2: "inactive" }; |
| 30 | const out = generateSuggestions( |
| 31 | statuses, |
| 32 | makePlugins(["p1"]), |
| 33 | makePlugins(["s1", "s2"]) |
| 34 | ); |
| 35 | expect(out.map((p) => p.slug)).toEqual(["p1", "s1", "s2"]); |
| 36 | }); |
| 37 | |
| 38 | it("skips active secondaries", () => { |
| 39 | const statuses = { p1: "inactive", s1: "active", s2: "inactive" }; |
| 40 | const out = generateSuggestions( |
| 41 | statuses, |
| 42 | makePlugins(["p1"]), |
| 43 | makePlugins(["s1", "s2"]) |
| 44 | ); |
| 45 | expect(out.map((p) => p.slug)).toEqual(["p1", "s2"]); |
| 46 | }); |
| 47 | |
| 48 | it("falls back to active primaries to fill remaining slots up to 4", () => { |
| 49 | const statuses = { p1: "active", p2: "active" }; |
| 50 | const out = generateSuggestions( |
| 51 | statuses, |
| 52 | makePlugins(["p1", "p2"]), |
| 53 | [] |
| 54 | ); |
| 55 | expect(out.map((p) => p.slug)).toEqual(["p1", "p2"]); |
| 56 | expect(out.every((p) => p.status === "active")).toBe(true); |
| 57 | }); |
| 58 | |
| 59 | it("caps the result at 4 items", () => { |
| 60 | const statuses = { |
| 61 | p1: "inactive", |
| 62 | p2: "inactive", |
| 63 | p3: "inactive", |
| 64 | p4: "inactive", |
| 65 | p5: "inactive", |
| 66 | }; |
| 67 | const out = generateSuggestions( |
| 68 | statuses, |
| 69 | makePlugins(["p1", "p2", "p3", "p4", "p5"]), |
| 70 | [] |
| 71 | ); |
| 72 | expect(out).toHaveLength(4); |
| 73 | expect(out.map((p) => p.slug)).toEqual(["p1", "p2", "p3", "p4"]); |
| 74 | }); |
| 75 | |
| 76 | it("preserves the original plugin fields alongside the status", () => { |
| 77 | const statuses = { p1: "inactive" }; |
| 78 | const out = generateSuggestions( |
| 79 | statuses, |
| 80 | makePlugins(["p1"]), |
| 81 | [] |
| 82 | ); |
| 83 | expect(out[0]).toMatchObject({ |
| 84 | slug: "p1", |
| 85 | init: "p1/p1.php", |
| 86 | name: "p1", |
| 87 | status: "inactive", |
| 88 | }); |
| 89 | }); |
| 90 | |
| 91 | it("returns an empty array when both lists are empty", () => { |
| 92 | expect(generateSuggestions({}, [], [])).toEqual([]); |
| 93 | }); |
| 94 | }); |
| 95 |