|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { |
| 3 | + createImageVariants, |
| 4 | + mergeImageVariantsByType, |
| 5 | + mergeImageVariantsToSrcSet, |
| 6 | +} from "../core/transformer"; |
| 7 | +import type { SolidImageSource, SolidImageTransformer, SolidImageVariant } from "../core/types"; |
| 8 | + |
| 9 | +describe("createImageVariants", () => { |
| 10 | + it("returns an array with 1 item when transformer returns a single variant", () => { |
| 11 | + const source: SolidImageSource<{}> = { |
| 12 | + source: "/img/photo.jpg", |
| 13 | + width: 800, |
| 14 | + height: 600, |
| 15 | + options: {}, |
| 16 | + }; |
| 17 | + |
| 18 | + const transformer: SolidImageTransformer<{}> = { |
| 19 | + transform: () => ({ |
| 20 | + path: "/img/photo-800.webp", |
| 21 | + width: 800, |
| 22 | + type: "image/webp", |
| 23 | + }), |
| 24 | + }; |
| 25 | + |
| 26 | + const result = createImageVariants(source, transformer); |
| 27 | + |
| 28 | + expect(result).toHaveLength(1); |
| 29 | + expect(result[0]).toEqual({ |
| 30 | + path: "/img/photo-800.webp", |
| 31 | + width: 800, |
| 32 | + type: "image/webp", |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + it("returns an array with 3 items when transformer returns 3 variants", () => { |
| 37 | + const source: SolidImageSource<{}> = { |
| 38 | + source: "/img/hero.png", |
| 39 | + width: 1200, |
| 40 | + height: 800, |
| 41 | + options: {}, |
| 42 | + }; |
| 43 | + |
| 44 | + const variants: SolidImageVariant[] = [ |
| 45 | + { path: "/img/hero-400.avif", width: 400, type: "image/avif" }, |
| 46 | + { path: "/img/hero-800.avif", width: 800, type: "image/avif" }, |
| 47 | + { path: "/img/hero-1200.avif", width: 1200, type: "image/avif" }, |
| 48 | + ]; |
| 49 | + |
| 50 | + const transformer: SolidImageTransformer<{}> = { |
| 51 | + transform: () => variants, |
| 52 | + }; |
| 53 | + |
| 54 | + const result = createImageVariants(source, transformer); |
| 55 | + |
| 56 | + expect(result).toHaveLength(3); |
| 57 | + expect(result[0]).toEqual({ |
| 58 | + path: "/img/hero-400.avif", |
| 59 | + width: 400, |
| 60 | + type: "image/avif", |
| 61 | + }); |
| 62 | + expect(result[1]).toEqual({ |
| 63 | + path: "/img/hero-800.avif", |
| 64 | + width: 800, |
| 65 | + type: "image/avif", |
| 66 | + }); |
| 67 | + expect(result[2]).toEqual({ |
| 68 | + path: "/img/hero-1200.avif", |
| 69 | + width: 1200, |
| 70 | + type: "image/avif", |
| 71 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + it("passes the source to the transformer", () => { |
| 75 | + const source: SolidImageSource<{ quality: number }> = { |
| 76 | + source: "/img/test.jpg", |
| 77 | + width: 500, |
| 78 | + height: 300, |
| 79 | + options: { quality: 80 }, |
| 80 | + }; |
| 81 | + |
| 82 | + let receivedSource: SolidImageSource<{ quality: number }> | undefined; |
| 83 | + |
| 84 | + const transformer: SolidImageTransformer<{ quality: number }> = { |
| 85 | + transform: src => { |
| 86 | + receivedSource = src; |
| 87 | + return { path: "/img/test-500.webp", width: 500, type: "image/webp" }; |
| 88 | + }, |
| 89 | + }; |
| 90 | + |
| 91 | + createImageVariants(source, transformer); |
| 92 | + |
| 93 | + expect(receivedSource).toBe(source); |
| 94 | + }); |
| 95 | +}); |
| 96 | + |
| 97 | +describe("mergeImageVariantsByType", () => { |
| 98 | + it("groups variants by their MIME type", () => { |
| 99 | + const variants: SolidImageVariant[] = [ |
| 100 | + { path: "/img/a-400.webp", width: 400, type: "image/webp" }, |
| 101 | + { path: "/img/a-800.webp", width: 800, type: "image/webp" }, |
| 102 | + { path: "/img/a-400.avif", width: 400, type: "image/avif" }, |
| 103 | + { path: "/img/a-800.avif", width: 800, type: "image/avif" }, |
| 104 | + ]; |
| 105 | + |
| 106 | + const result = mergeImageVariantsByType(variants); |
| 107 | + |
| 108 | + expect(result.size).toBe(2); |
| 109 | + |
| 110 | + const webpGroup = result.get("image/webp")!; |
| 111 | + expect(webpGroup).toHaveLength(2); |
| 112 | + expect(webpGroup[0]).toEqual({ |
| 113 | + path: "/img/a-400.webp", |
| 114 | + width: 400, |
| 115 | + type: "image/webp", |
| 116 | + }); |
| 117 | + expect(webpGroup[1]).toEqual({ |
| 118 | + path: "/img/a-800.webp", |
| 119 | + width: 800, |
| 120 | + type: "image/webp", |
| 121 | + }); |
| 122 | + |
| 123 | + const avifGroup = result.get("image/avif")!; |
| 124 | + expect(avifGroup).toHaveLength(2); |
| 125 | + expect(avifGroup[0]).toEqual({ |
| 126 | + path: "/img/a-400.avif", |
| 127 | + width: 400, |
| 128 | + type: "image/avif", |
| 129 | + }); |
| 130 | + expect(avifGroup[1]).toEqual({ |
| 131 | + path: "/img/a-800.avif", |
| 132 | + width: 800, |
| 133 | + type: "image/avif", |
| 134 | + }); |
| 135 | + }); |
| 136 | + |
| 137 | + it("returns a Map with 1 entry when all variants share the same type", () => { |
| 138 | + const variants: SolidImageVariant[] = [ |
| 139 | + { path: "/img/x-100.png", width: 100, type: "image/png" }, |
| 140 | + { path: "/img/x-200.png", width: 200, type: "image/png" }, |
| 141 | + ]; |
| 142 | + |
| 143 | + const result = mergeImageVariantsByType(variants); |
| 144 | + |
| 145 | + expect(result.size).toBe(1); |
| 146 | + expect(result.get("image/png")).toHaveLength(2); |
| 147 | + }); |
| 148 | +}); |
| 149 | + |
| 150 | +describe("mergeImageVariantsToSrcSet", () => { |
| 151 | + it("formats a single variant into a srcset string", () => { |
| 152 | + const variants: SolidImageVariant[] = [ |
| 153 | + { path: "/img/photo-100.webp", width: 100, type: "image/webp" }, |
| 154 | + ]; |
| 155 | + |
| 156 | + const result = mergeImageVariantsToSrcSet(variants); |
| 157 | + |
| 158 | + expect(result).toBe("/img/photo-100.webp 100w"); |
| 159 | + }); |
| 160 | + |
| 161 | + it("formats multiple variants into a comma-separated srcset string", () => { |
| 162 | + const variants: SolidImageVariant[] = [ |
| 163 | + { path: "/img/photo-100.webp", width: 100, type: "image/webp" }, |
| 164 | + { path: "/img/photo-200.webp", width: 200, type: "image/webp" }, |
| 165 | + { path: "/img/photo-400.webp", width: 400, type: "image/webp" }, |
| 166 | + ]; |
| 167 | + |
| 168 | + const result = mergeImageVariantsToSrcSet(variants); |
| 169 | + |
| 170 | + expect(result).toBe( |
| 171 | + "/img/photo-100.webp 100w,/img/photo-200.webp 200w,/img/photo-400.webp 400w", |
| 172 | + ); |
| 173 | + }); |
| 174 | +}); |
0 commit comments