|
| 1 | +import { WebIcon, WebImage } from "mendix"; |
| 2 | +import { dynamic } from "@mendix/widget-plugin-test-utils"; |
| 3 | +import { getImageProps, GetImagePropsInput } from "../getImageProps"; |
| 4 | + |
| 5 | +const webImage = (uri: string): WebImage => ({ uri, name: "test.jpg" }); |
| 6 | + |
| 7 | +describe("getImageProps", () => { |
| 8 | + describe('datasource: "image"', () => { |
| 9 | + it("returns the main image URI when main image is available", () => { |
| 10 | + const input: GetImagePropsInput = { |
| 11 | + datasource: "image", |
| 12 | + imageObject: dynamic.available(webImage("https://example.com/main.jpg")) |
| 13 | + }; |
| 14 | + expect(getImageProps(input)).toEqual({ type: "image", image: "https://example.com/main.jpg" }); |
| 15 | + }); |
| 16 | + |
| 17 | + it("returns the main image URI when main image is loading (uri present)", () => { |
| 18 | + const input: GetImagePropsInput = { |
| 19 | + datasource: "image", |
| 20 | + imageObject: dynamic.loading(webImage("https://example.com/main.jpg")) |
| 21 | + }; |
| 22 | + expect(getImageProps(input)).toEqual({ type: "image", image: "https://example.com/main.jpg" }); |
| 23 | + }); |
| 24 | + |
| 25 | + it("returns undefined image when main image is loading (no uri yet)", () => { |
| 26 | + const input: GetImagePropsInput = { |
| 27 | + datasource: "image", |
| 28 | + imageObject: dynamic.loading<WebImage>() |
| 29 | + }; |
| 30 | + expect(getImageProps(input)).toEqual({ type: "image", image: undefined }); |
| 31 | + }); |
| 32 | + |
| 33 | + it("falls back to defaultImage when main image is unavailable and fallback is available", () => { |
| 34 | + const input: GetImagePropsInput = { |
| 35 | + datasource: "image", |
| 36 | + imageObject: dynamic.unavailable<WebImage>(), |
| 37 | + defaultImageDynamic: dynamic.available(webImage("https://example.com/fallback.jpg")) |
| 38 | + }; |
| 39 | + expect(getImageProps(input)).toEqual({ type: "image", image: "https://example.com/fallback.jpg" }); |
| 40 | + }); |
| 41 | + |
| 42 | + it("falls back to defaultImage when main image is unavailable and fallback is loading", () => { |
| 43 | + const input: GetImagePropsInput = { |
| 44 | + datasource: "image", |
| 45 | + imageObject: dynamic.unavailable<WebImage>(), |
| 46 | + defaultImageDynamic: dynamic.loading(webImage("https://example.com/fallback.jpg")) |
| 47 | + }; |
| 48 | + expect(getImageProps(input)).toEqual({ type: "image", image: "https://example.com/fallback.jpg" }); |
| 49 | + }); |
| 50 | + |
| 51 | + it("returns undefined image when both main image and fallback are unavailable", () => { |
| 52 | + const input: GetImagePropsInput = { |
| 53 | + datasource: "image", |
| 54 | + imageObject: dynamic.unavailable<WebImage>(), |
| 55 | + defaultImageDynamic: dynamic.unavailable<WebImage>() |
| 56 | + }; |
| 57 | + expect(getImageProps(input)).toEqual({ type: "image", image: undefined }); |
| 58 | + }); |
| 59 | + |
| 60 | + it("returns undefined image when imageObject and defaultImageDynamic are not provided", () => { |
| 61 | + const input: GetImagePropsInput = { datasource: "image" }; |
| 62 | + expect(getImageProps(input)).toEqual({ type: "image", image: undefined }); |
| 63 | + }); |
| 64 | + |
| 65 | + it("does not use defaultImage when main image is available", () => { |
| 66 | + const input: GetImagePropsInput = { |
| 67 | + datasource: "image", |
| 68 | + imageObject: dynamic.available(webImage("https://example.com/main.jpg")), |
| 69 | + defaultImageDynamic: dynamic.available(webImage("https://example.com/fallback.jpg")) |
| 70 | + }; |
| 71 | + expect(getImageProps(input)).toEqual({ type: "image", image: "https://example.com/main.jpg" }); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + describe('datasource: "imageUrl"', () => { |
| 76 | + it("returns the URL when imageUrl is available", () => { |
| 77 | + const input: GetImagePropsInput = { |
| 78 | + datasource: "imageUrl", |
| 79 | + imageUrl: dynamic.available("https://example.com/image.jpg") |
| 80 | + }; |
| 81 | + expect(getImageProps(input)).toEqual({ type: "image", image: "https://example.com/image.jpg" }); |
| 82 | + }); |
| 83 | + |
| 84 | + it("returns stale URL when imageUrl is loading with a cached value", () => { |
| 85 | + const input: GetImagePropsInput = { |
| 86 | + datasource: "imageUrl", |
| 87 | + imageUrl: dynamic.loading("https://example.com/image.jpg") |
| 88 | + }; |
| 89 | + expect(getImageProps(input)).toEqual({ type: "image", image: "https://example.com/image.jpg" }); |
| 90 | + }); |
| 91 | + |
| 92 | + it("returns undefined image when imageUrl is loading with no cached value", () => { |
| 93 | + const input: GetImagePropsInput = { |
| 94 | + datasource: "imageUrl", |
| 95 | + imageUrl: dynamic.loading<string>() |
| 96 | + }; |
| 97 | + expect(getImageProps(input)).toEqual({ type: "image", image: undefined }); |
| 98 | + }); |
| 99 | + |
| 100 | + it("returns undefined image when imageUrl is unavailable", () => { |
| 101 | + const input: GetImagePropsInput = { |
| 102 | + datasource: "imageUrl", |
| 103 | + imageUrl: dynamic.unavailable<string>() |
| 104 | + }; |
| 105 | + expect(getImageProps(input)).toEqual({ type: "image", image: undefined }); |
| 106 | + }); |
| 107 | + |
| 108 | + it("returns undefined image when imageUrl is not provided", () => { |
| 109 | + const input: GetImagePropsInput = { datasource: "imageUrl" }; |
| 110 | + expect(getImageProps(input)).toEqual({ type: "image", image: undefined }); |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + describe('datasource: "icon"', () => { |
| 115 | + it("returns glyph icon class when a glyph icon is available", () => { |
| 116 | + const glyphIcon: WebIcon = { type: "glyph", iconClass: "glyphicon-star" }; |
| 117 | + const input: GetImagePropsInput = { |
| 118 | + datasource: "icon", |
| 119 | + imageIcon: dynamic.available(glyphIcon) |
| 120 | + }; |
| 121 | + expect(getImageProps(input)).toEqual({ type: "glyph", image: "glyphicon-star" }); |
| 122 | + }); |
| 123 | + |
| 124 | + it("returns image icon URL when an image icon is available", () => { |
| 125 | + const imageIcon: WebIcon = { type: "image", iconUrl: "https://example.com/icon.png" }; |
| 126 | + const input: GetImagePropsInput = { |
| 127 | + datasource: "icon", |
| 128 | + imageIcon: dynamic.available(imageIcon) |
| 129 | + }; |
| 130 | + expect(getImageProps(input)).toEqual({ type: "image", image: "https://example.com/icon.png" }); |
| 131 | + }); |
| 132 | + |
| 133 | + it("returns mx-icon class when a named icon is available", () => { |
| 134 | + const namedIcon: WebIcon = { type: "icon", iconClass: "mx-icon-star" }; |
| 135 | + const input: GetImagePropsInput = { |
| 136 | + datasource: "icon", |
| 137 | + imageIcon: dynamic.available(namedIcon) |
| 138 | + }; |
| 139 | + expect(getImageProps(input)).toEqual({ type: "icon", image: "mx-icon-star" }); |
| 140 | + }); |
| 141 | + |
| 142 | + it("returns fallback when imageIcon is loading", () => { |
| 143 | + const input: GetImagePropsInput = { |
| 144 | + datasource: "icon", |
| 145 | + imageIcon: dynamic.loading<WebIcon>() |
| 146 | + }; |
| 147 | + expect(getImageProps(input)).toEqual({ type: "image", image: undefined }); |
| 148 | + }); |
| 149 | + |
| 150 | + it("returns fallback when imageIcon is unavailable", () => { |
| 151 | + const input: GetImagePropsInput = { |
| 152 | + datasource: "icon", |
| 153 | + imageIcon: dynamic.unavailable<WebIcon>() |
| 154 | + }; |
| 155 | + expect(getImageProps(input)).toEqual({ type: "image", image: undefined }); |
| 156 | + }); |
| 157 | + |
| 158 | + it("returns fallback when imageIcon value is undefined (WebIcon = undefined)", () => { |
| 159 | + const input: GetImagePropsInput = { |
| 160 | + datasource: "icon", |
| 161 | + imageIcon: dynamic.available(undefined as WebIcon) |
| 162 | + }; |
| 163 | + expect(getImageProps(input)).toEqual({ type: "image", image: undefined }); |
| 164 | + }); |
| 165 | + |
| 166 | + it("returns fallback when imageIcon is not provided", () => { |
| 167 | + const input: GetImagePropsInput = { datasource: "icon" }; |
| 168 | + expect(getImageProps(input)).toEqual({ type: "image", image: undefined }); |
| 169 | + }); |
| 170 | + }); |
| 171 | + |
| 172 | + describe("unknown datasource", () => { |
| 173 | + it("returns fallback for an unrecognised datasource value", () => { |
| 174 | + const input = { datasource: "unknown" as GetImagePropsInput["datasource"] }; |
| 175 | + expect(getImageProps(input)).toEqual({ type: "image", image: undefined }); |
| 176 | + }); |
| 177 | + }); |
| 178 | +}); |
0 commit comments