|
| 1 | +import { MediaContract } from "@paperbits/common/media"; |
| 2 | +import { MediaPermalinkResolver } from "@paperbits/common/media/mediaPermalinkResolver.publish"; |
| 3 | +import { PictureViewModelBinder } from "../src/picture/ko/pictureViewModelBinder"; |
| 4 | +import { PictureModelBinder } from "../src/picture/pictureModelBinder"; |
| 5 | +import { expect } from "chai"; |
| 6 | + |
| 7 | + |
| 8 | +describe("Media permalink resolver", async () => { |
| 9 | + const styleCompiler: any = { |
| 10 | + getStyleModelAsync: (styles: any) => { |
| 11 | + return Promise.resolve({}); |
| 12 | + } |
| 13 | + } |
| 14 | + |
| 15 | + const pictureContract: any = { |
| 16 | + type: "picture", |
| 17 | + caption: "Logo", |
| 18 | + width: "150px", |
| 19 | + height: "40px", |
| 20 | + sourceKey: "uploads/7aa4ab1f-cf14-ef3c-c4c5-420434f4de9e" |
| 21 | + } |
| 22 | + |
| 23 | + it("Correctly resolves permalink for uploaded media.", async () => { |
| 24 | + const mediaService: any = { |
| 25 | + getMediaByKey: (mediaKey: string): Promise<MediaContract> => { |
| 26 | + const mediaContract: MediaContract = { |
| 27 | + key: "uploads/7aa4ab1f-cf14-ef3c-c4c5-420434f4de9e", |
| 28 | + fileName: "your-specs-logo.svg", |
| 29 | + blobKey: "7aa4ab1f-cf14-ef3c-c4c5-420434f4de9e", |
| 30 | + description: "Company logo", |
| 31 | + keywords: "logo", |
| 32 | + permalink: "/content/your-specs-logo.svg", |
| 33 | + mimeType: "image/svg+xml", |
| 34 | + variants: [] |
| 35 | + } |
| 36 | + |
| 37 | + return Promise.resolve(mediaContract); |
| 38 | + } |
| 39 | + }; |
| 40 | + |
| 41 | + const permalinkResolver = new MediaPermalinkResolver(mediaService); |
| 42 | + const pictureViewModelBinder = new PictureViewModelBinder(styleCompiler, permalinkResolver, mediaService); |
| 43 | + const modelBinder = new PictureModelBinder(permalinkResolver); |
| 44 | + |
| 45 | + const pictureModel = await modelBinder.contractToModel(pictureContract, null); |
| 46 | + const pictureWidgetState: any = {}; |
| 47 | + await pictureViewModelBinder.modelToState(pictureModel, pictureWidgetState); |
| 48 | + |
| 49 | + expect(pictureWidgetState.sourceUrl).to.be.equal("/content/your-specs-logo.svg"); |
| 50 | + }); |
| 51 | + |
| 52 | + it("Correctly resolves permalink for referenced media.", async () => { |
| 53 | + const mediaService: any = { |
| 54 | + getMediaByKey: (mediaKey: string): Promise<MediaContract> => { |
| 55 | + const mediaContract: MediaContract = { |
| 56 | + key: "uploads/7aa4ab1f-cf14-ef3c-c4c5-420434f4de9e", |
| 57 | + fileName: "your-specs-logo.svg", |
| 58 | + downloadUrl: "https://content/your-specs-logo.svg", |
| 59 | + description: "Company logo", |
| 60 | + keywords: "logo", |
| 61 | + permalink: "/content/your-specs-logo.svg", |
| 62 | + mimeType: "image/svg+xml", |
| 63 | + variants: [] |
| 64 | + } |
| 65 | + |
| 66 | + return Promise.resolve(mediaContract); |
| 67 | + } |
| 68 | + }; |
| 69 | + |
| 70 | + const permalinkResolver = new MediaPermalinkResolver(mediaService); |
| 71 | + const pictureViewModelBinder = new PictureViewModelBinder(styleCompiler, permalinkResolver, mediaService); |
| 72 | + const modelBinder = new PictureModelBinder(permalinkResolver); |
| 73 | + |
| 74 | + const pictureModel = await modelBinder.contractToModel(pictureContract, null); |
| 75 | + const pictureWidgetState: any = {}; |
| 76 | + await pictureViewModelBinder.modelToState(pictureModel, pictureWidgetState); |
| 77 | + |
| 78 | + expect(pictureWidgetState.sourceUrl).to.be.equal("/content/your-specs-logo.svg"); |
| 79 | + }); |
| 80 | +}); |
0 commit comments