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