|
| 1 | +import { module, test } from "qunit"; |
| 2 | +import migrate from "../../../../migrations/settings/0001-migrate-nav-links-to-objects"; |
| 3 | + |
| 4 | +module( |
| 5 | + "Custom Top Navigation Links | Migrations | Settings | 0001-migrate-nav-links-to-objects", |
| 6 | + function () { |
| 7 | + test("does nothing when the old setting is not present", function (assert) { |
| 8 | + const settings = new Map(Object.entries({ Hide_dropdowns: true })); |
| 9 | + |
| 10 | + const result = migrate(settings); |
| 11 | + |
| 12 | + assert.deepEqual( |
| 13 | + Object.fromEntries(result.entries()), |
| 14 | + Object.fromEntries(new Map(Object.entries({ Hide_dropdowns: true }))) |
| 15 | + ); |
| 16 | + }); |
| 17 | + |
| 18 | + test("migrate when value of setting is already an array", function (assert) { |
| 19 | + const links = [ |
| 20 | + { |
| 21 | + display_name: "Latest", |
| 22 | + title: "topics with recent posts", |
| 23 | + url: "/latest", |
| 24 | + }, |
| 25 | + ]; |
| 26 | + const settings = new Map(Object.entries({ Nav_links: links })); |
| 27 | + |
| 28 | + const result = migrate(settings); |
| 29 | + |
| 30 | + assert.deepEqual( |
| 31 | + Object.fromEntries(result.entries()), |
| 32 | + Object.fromEntries(new Map(Object.entries({ nav_links: links }))) |
| 33 | + ); |
| 34 | + }); |
| 35 | + |
| 36 | + test("migrate when old setting value is an empty string", function (assert) { |
| 37 | + const settings = new Map(Object.entries({ Nav_links: "" })); |
| 38 | + |
| 39 | + const result = migrate(settings); |
| 40 | + |
| 41 | + assert.deepEqual( |
| 42 | + Object.fromEntries(result.entries()), |
| 43 | + Object.fromEntries(new Map(Object.entries({ nav_links: [] }))) |
| 44 | + ); |
| 45 | + }); |
| 46 | + |
| 47 | + test("migrate when old setting value is invalid", function (assert) { |
| 48 | + const settings = new Map( |
| 49 | + Object.entries({ |
| 50 | + Nav_links: "Missing URL|Missing URL 2;some title|;;/no-name", |
| 51 | + }) |
| 52 | + ); |
| 53 | + |
| 54 | + const result = migrate(settings); |
| 55 | + |
| 56 | + assert.deepEqual( |
| 57 | + Object.fromEntries(result.entries()), |
| 58 | + Object.fromEntries(new Map(Object.entries({ nav_links: [] }))) |
| 59 | + ); |
| 60 | + }); |
| 61 | + |
| 62 | + test("migrate when title is not provided", function (assert) { |
| 63 | + const settings = new Map( |
| 64 | + Object.entries({ |
| 65 | + Nav_links: "External link;;https://meta.discourse.org", |
| 66 | + }) |
| 67 | + ); |
| 68 | + |
| 69 | + const result = migrate(settings); |
| 70 | + |
| 71 | + assert.deepEqual( |
| 72 | + Object.fromEntries(result.entries()), |
| 73 | + Object.fromEntries( |
| 74 | + new Map( |
| 75 | + Object.entries({ |
| 76 | + nav_links: [ |
| 77 | + { |
| 78 | + display_name: "External link", |
| 79 | + url: "https://meta.discourse.org", |
| 80 | + }, |
| 81 | + ], |
| 82 | + }) |
| 83 | + ) |
| 84 | + ) |
| 85 | + ); |
| 86 | + }); |
| 87 | + |
| 88 | + test("migrate", function (assert) { |
| 89 | + const settings = new Map( |
| 90 | + Object.entries({ |
| 91 | + Nav_links: |
| 92 | + "Latest;topics with recent posts;/latest|Categories;all topics grouped by category;/categories|Top;the most active topics in the last year, month, week or day;/top", |
| 93 | + }) |
| 94 | + ); |
| 95 | + |
| 96 | + const result = migrate(settings); |
| 97 | + |
| 98 | + assert.deepEqual( |
| 99 | + Object.fromEntries(result.entries()), |
| 100 | + Object.fromEntries( |
| 101 | + new Map( |
| 102 | + Object.entries({ |
| 103 | + nav_links: [ |
| 104 | + { |
| 105 | + display_name: "Latest", |
| 106 | + title: "topics with recent posts", |
| 107 | + url: "/latest", |
| 108 | + }, |
| 109 | + { |
| 110 | + display_name: "Categories", |
| 111 | + title: "all topics grouped by category", |
| 112 | + url: "/categories", |
| 113 | + }, |
| 114 | + { |
| 115 | + display_name: "Top", |
| 116 | + title: |
| 117 | + "the most active topics in the last year, month, week or day", |
| 118 | + url: "/top", |
| 119 | + }, |
| 120 | + ], |
| 121 | + }) |
| 122 | + ) |
| 123 | + ) |
| 124 | + ); |
| 125 | + }); |
| 126 | + } |
| 127 | +); |
0 commit comments