|
| 1 | +import { fromMetaData as _fromMetaData } from "../src/index"; |
| 2 | +import { describe, it, expect, beforeEach, afterEach, vi } from "vitest"; |
| 3 | + |
| 4 | +let fromMetaData = _fromMetaData; |
| 5 | +if (import.meta.env.TEST_BUILD) { |
| 6 | + try { |
| 7 | + fromMetaData = require("../dist/index").fromMetaData; |
| 8 | + } catch (_) {} |
| 9 | +} |
| 10 | + |
| 11 | +describe("fromMetaData", () => { |
| 12 | + let warn = console.warn; |
| 13 | + beforeEach(() => { |
| 14 | + console.warn = vi.fn(); |
| 15 | + }); |
| 16 | + afterEach(() => { |
| 17 | + console.warn = warn; |
| 18 | + }); |
| 19 | + |
| 20 | + it("does not create a descriptor from empty object", () => { |
| 21 | + let descriptors = fromMetaData({}); |
| 22 | + expect(descriptors).toEqual([]); |
| 23 | + }); |
| 24 | + |
| 25 | + it("does not create a descriptor from 'undefined' value", () => { |
| 26 | + let descriptors = fromMetaData({ "og:type": undefined }); |
| 27 | + expect(descriptors).toEqual([]); |
| 28 | + }); |
| 29 | + |
| 30 | + it("does not create a descriptor from 'null' value", () => { |
| 31 | + // @ts-expect-error |
| 32 | + let descriptors = fromMetaData({ title: null }); |
| 33 | + expect(descriptors).toEqual([]); |
| 34 | + }); |
| 35 | + |
| 36 | + it("creates multiple descriptors", () => { |
| 37 | + let descriptors = fromMetaData({ |
| 38 | + title: "hiya | my website", |
| 39 | + "og:title": "hiya", |
| 40 | + description: "my website", |
| 41 | + "og:image": [ |
| 42 | + "https://example.com/image1.jpg", |
| 43 | + "https://example.com/image2.jpg", |
| 44 | + ], |
| 45 | + }); |
| 46 | + expect(descriptors).toEqual([ |
| 47 | + { title: "hiya | my website", key: expect.any(String) }, |
| 48 | + { property: "og:title", content: "hiya", key: expect.any(String) }, |
| 49 | + { name: "description", content: "my website", key: expect.any(String) }, |
| 50 | + { |
| 51 | + property: "og:image", |
| 52 | + content: "https://example.com/image1.jpg", |
| 53 | + key: expect.any(String), |
| 54 | + }, |
| 55 | + { |
| 56 | + property: "og:image", |
| 57 | + content: "https://example.com/image2.jpg", |
| 58 | + key: expect.any(String), |
| 59 | + }, |
| 60 | + ]); |
| 61 | + }); |
| 62 | + |
| 63 | + describe("title tags", () => { |
| 64 | + it("creates a 'title' descriptor from 'title' property", () => { |
| 65 | + let descriptors = fromMetaData({ title: "hello world" }); |
| 66 | + expect(descriptors[0]).toEqual({ |
| 67 | + title: "hello world", |
| 68 | + key: "title", |
| 69 | + }); |
| 70 | + }); |
| 71 | + }); |
| 72 | + |
| 73 | + describe("'charSet' meta tags", () => { |
| 74 | + it("creates a 'charSet' descriptor from 'charSet' property", () => { |
| 75 | + let descriptors = fromMetaData({ charSet: "utf-8" }); |
| 76 | + expect(descriptors[0]).toEqual({ |
| 77 | + charSet: "utf-8", |
| 78 | + key: "charSet", |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + it("creates a 'charSet' descriptor from 'charset' property", () => { |
| 83 | + let descriptors = fromMetaData({ charset: "utf-8" }); |
| 84 | + expect(descriptors[0]).toEqual({ charSet: "utf-8", key: "charSet" }); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe("og:* meta tags", () => { |
| 89 | + it("creates an 'og:type' descriptor from 'og:type' property", () => { |
| 90 | + let descriptors = fromMetaData({ "og:type": "website" }); |
| 91 | + expect(descriptors[0]).toEqual({ |
| 92 | + property: "og:type", |
| 93 | + content: "website", |
| 94 | + key: expect.any(String), |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + it("creates an 'og:title' descriptor from 'og:title' property", () => { |
| 99 | + let descriptors = fromMetaData({ "og:title": "hello world" }); |
| 100 | + expect(descriptors[0]).toEqual({ |
| 101 | + property: "og:title", |
| 102 | + content: "hello world", |
| 103 | + key: expect.any(String), |
| 104 | + }); |
| 105 | + }); |
| 106 | + |
| 107 | + it("creates an 'og:description' descriptor from 'og:description' property", () => { |
| 108 | + let descriptors = fromMetaData({ "og:description": "hello world" }); |
| 109 | + expect(descriptors[0]).toEqual({ |
| 110 | + property: "og:description", |
| 111 | + content: "hello world", |
| 112 | + key: expect.any(String), |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + it("creates an 'og:url' descriptor from 'og:url' property", () => { |
| 117 | + let descriptors = fromMetaData({ "og:url": "https://example.com" }); |
| 118 | + expect(descriptors[0]).toEqual({ |
| 119 | + property: "og:url", |
| 120 | + content: "https://example.com", |
| 121 | + key: expect.any(String), |
| 122 | + }); |
| 123 | + }); |
| 124 | + |
| 125 | + it("creates multiple descriptors with array values", () => { |
| 126 | + let descriptors = fromMetaData({ |
| 127 | + "og:image": [ |
| 128 | + "https://example.com/image1.png", |
| 129 | + "https://example.com/image2.png", |
| 130 | + ], |
| 131 | + }); |
| 132 | + expect(descriptors).toEqual([ |
| 133 | + { |
| 134 | + property: "og:image", |
| 135 | + content: "https://example.com/image1.png", |
| 136 | + key: expect.any(String), |
| 137 | + }, |
| 138 | + { |
| 139 | + property: "og:image", |
| 140 | + content: "https://example.com/image2.png", |
| 141 | + key: expect.any(String), |
| 142 | + }, |
| 143 | + ]); |
| 144 | + }); |
| 145 | + }); |
| 146 | + |
| 147 | + describe("fb:* meta tags", () => { |
| 148 | + it("creates a 'fb:app_id' descriptor from 'fb:app_id' property", () => { |
| 149 | + let descriptors = fromMetaData({ "fb:app_id": "123" }); |
| 150 | + expect(descriptors[0]).toEqual({ |
| 151 | + property: "fb:app_id", |
| 152 | + content: "123", |
| 153 | + key: expect.any(String), |
| 154 | + }); |
| 155 | + }); |
| 156 | + }); |
| 157 | + |
| 158 | + describe("twitter:* meta tags", () => { |
| 159 | + it("creates a 'twitter:card' descriptor from 'twitter:card' property", () => { |
| 160 | + let descriptors = fromMetaData({ "twitter:card": "summary" }); |
| 161 | + expect(descriptors[0]).toEqual({ |
| 162 | + name: "twitter:card", |
| 163 | + content: "summary", |
| 164 | + key: expect.any(String), |
| 165 | + }); |
| 166 | + }); |
| 167 | + |
| 168 | + it("creates a 'twitter:site' descriptor from 'twitter:site' property", () => { |
| 169 | + let descriptors = fromMetaData({ "twitter:site": "@example" }); |
| 170 | + expect(descriptors[0]).toEqual({ |
| 171 | + name: "twitter:site", |
| 172 | + content: "@example", |
| 173 | + key: expect.any(String), |
| 174 | + }); |
| 175 | + }); |
| 176 | + |
| 177 | + it("creates a 'twitter:title' descriptor from 'twitter:title' property", () => { |
| 178 | + let descriptors = fromMetaData({ "twitter:title": "hello world" }); |
| 179 | + expect(descriptors[0]).toEqual({ |
| 180 | + name: "twitter:title", |
| 181 | + content: "hello world", |
| 182 | + key: expect.any(String), |
| 183 | + }); |
| 184 | + }); |
| 185 | + }); |
| 186 | + |
| 187 | + describe("standard meta tags", () => { |
| 188 | + it("creates a 'description' descriptor from 'description' property", () => { |
| 189 | + let descriptors = fromMetaData({ description: "hello world" }); |
| 190 | + expect(descriptors[0]).toEqual({ |
| 191 | + name: "description", |
| 192 | + content: "hello world", |
| 193 | + key: expect.any(String), |
| 194 | + }); |
| 195 | + }); |
| 196 | + |
| 197 | + it("creates a 'keywords' descriptor from 'keywords' property", () => { |
| 198 | + let descriptors = fromMetaData({ keywords: "hello,world" }); |
| 199 | + expect(descriptors[0]).toEqual({ |
| 200 | + name: "keywords", |
| 201 | + content: "hello,world", |
| 202 | + key: expect.any(String), |
| 203 | + }); |
| 204 | + }); |
| 205 | + |
| 206 | + it("creates a 'viewport' descriptor from 'viewport' property", () => { |
| 207 | + let descriptors = fromMetaData({ |
| 208 | + viewport: "width=device-width, initial-scale=1", |
| 209 | + }); |
| 210 | + expect(descriptors[0]).toEqual({ |
| 211 | + name: "viewport", |
| 212 | + content: "width=device-width, initial-scale=1", |
| 213 | + key: expect.any(String), |
| 214 | + }); |
| 215 | + }); |
| 216 | + |
| 217 | + it("stringifies numeric value", () => { |
| 218 | + // @ts-expect-error |
| 219 | + let descriptors = fromMetaData({ description: 1 }); |
| 220 | + expect(descriptors[0]).toEqual({ |
| 221 | + name: "description", |
| 222 | + content: "1", |
| 223 | + key: expect.any(String), |
| 224 | + }); |
| 225 | + }); |
| 226 | + |
| 227 | + it("does not create a descriptor if value is 'falsey'", () => { |
| 228 | + // This includes any 'falsey' value, including numbers. Ideally those |
| 229 | + // would be stringified and work just fine since that's how React handles |
| 230 | + // rendering falsey numeric values, but that's not how it works in v1 so |
| 231 | + // it works the same way here. |
| 232 | + // @ts-expect-error |
| 233 | + let descriptors = fromMetaData({ description: 0 }); |
| 234 | + expect(descriptors).toEqual([]); |
| 235 | + }); |
| 236 | + }); |
| 237 | + |
| 238 | + describe("arbitrary meta tags", () => { |
| 239 | + it("creates a descriptor from keys with object value", () => { |
| 240 | + let descriptors = fromMetaData({ |
| 241 | + refresh: { |
| 242 | + httpEquiv: "refresh", |
| 243 | + content: "3;url=https://www.mozilla.org", |
| 244 | + }, |
| 245 | + }); |
| 246 | + expect(descriptors[0]).toEqual({ |
| 247 | + httpEquiv: "refresh", |
| 248 | + content: "3;url=https://www.mozilla.org", |
| 249 | + key: expect.any(String), |
| 250 | + }); |
| 251 | + }); |
| 252 | + }); |
| 253 | +}); |
0 commit comments