|
| 1 | +import EventEmitter from "wolfy87-eventemitter"; |
| 2 | + |
| 3 | +import AssetSidebarWidget from "../src/AssetSidebarWidget"; |
| 4 | +import { IAssetSidebarInitData, LocationType } from "../src/types"; |
| 5 | +import Asset from "../src/stack/api/asset"; |
| 6 | + |
| 7 | +jest.mock("post-robot", () => ({ |
| 8 | + __esModule: true, |
| 9 | + default: { |
| 10 | + sendToParent: jest.fn(), |
| 11 | + }, |
| 12 | +})); |
| 13 | + |
| 14 | +jest.mock("../src/stack/api/asset"); |
| 15 | + |
| 16 | +describe("AssetSidebarWidget", () => { |
| 17 | + let assetSidebarWidget: AssetSidebarWidget; |
| 18 | + let mockInitData: IAssetSidebarInitData = { |
| 19 | + type: LocationType.ASSET_SIDEBAR_WIDGET, |
| 20 | + currentAsset: {} as any, |
| 21 | + config: {}, |
| 22 | + app_id: "mock_app_uid", |
| 23 | + installation_uid: "mock_installation_uid", |
| 24 | + extension_uid: "mock_extension_uid", |
| 25 | + stack: {} as any, |
| 26 | + user: {} as any, |
| 27 | + currentBranch: "mock_branch", |
| 28 | + region: "region", |
| 29 | + }; |
| 30 | + |
| 31 | + let connection: { sendToParent: (...props: any[]) => any }; |
| 32 | + let sendToParent; |
| 33 | + let emitter: EventEmitter; |
| 34 | + |
| 35 | + beforeEach(function () { |
| 36 | + sendToParent = function () { |
| 37 | + return Promise.resolve({ data: {} }); |
| 38 | + }; |
| 39 | + |
| 40 | + emitter = { |
| 41 | + on: (_event, cbf) => { |
| 42 | + setTimeout(() => { |
| 43 | + cbf({ state: "full_width" }); |
| 44 | + }, 50); |
| 45 | + }, |
| 46 | + } as EventEmitter; |
| 47 | + |
| 48 | + jest.spyOn(emitter, "on"); |
| 49 | + |
| 50 | + connection = { sendToParent }; |
| 51 | + jest.spyOn(connection, "sendToParent"); |
| 52 | + assetSidebarWidget = new AssetSidebarWidget( |
| 53 | + mockInitData as IAssetSidebarInitData, |
| 54 | + connection as any, |
| 55 | + emitter |
| 56 | + ); |
| 57 | + }); |
| 58 | + |
| 59 | + afterEach(() => { |
| 60 | + jest.resetAllMocks(); |
| 61 | + }); |
| 62 | + |
| 63 | + it("should set instance properties in constructor", () => { |
| 64 | + expect(assetSidebarWidget.currentAsset).toBe(mockInitData.currentAsset); |
| 65 | + expect(assetSidebarWidget._emitter).toBe(emitter); |
| 66 | + expect(assetSidebarWidget._connection).toBe(connection); |
| 67 | + }); |
| 68 | + |
| 69 | + describe("getData", () => { |
| 70 | + it("should return the current asset", () => { |
| 71 | + const currentAsset = assetSidebarWidget.getData(); |
| 72 | + expect(currentAsset).toBe(mockInitData.currentAsset); |
| 73 | + }); |
| 74 | + }); |
| 75 | + |
| 76 | + describe("setData", () => { |
| 77 | + it("should set the current asset with the one provided", async () => { |
| 78 | + const asset = {}; |
| 79 | + const result = await assetSidebarWidget.setData(asset); |
| 80 | + expect(connection.sendToParent).toHaveBeenCalledWith( |
| 81 | + "setData", |
| 82 | + asset |
| 83 | + ); |
| 84 | + expect(result).toEqual(undefined); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe("syncAsset", () => { |
| 89 | + it("should sync the upstream asset with the current", async () => { |
| 90 | + const result = await assetSidebarWidget.syncAsset(); |
| 91 | + expect(connection.sendToParent).toHaveBeenCalledWith("syncAsset"); |
| 92 | + expect(result).toEqual(undefined); |
| 93 | + }); |
| 94 | + }); |
| 95 | + |
| 96 | + describe("updateWidth", () => { |
| 97 | + it("should throw an error if width is invalid", () => { |
| 98 | + const error = new Error("Width must be a number"); |
| 99 | + expect( |
| 100 | + assetSidebarWidget.updateWidth("500" as any) |
| 101 | + ).rejects.toThrowError(error); |
| 102 | + }); |
| 103 | + |
| 104 | + it("should update the width with the one provided", async () => { |
| 105 | + const mockWidth = 500; |
| 106 | + const result = await assetSidebarWidget.updateWidth(mockWidth); |
| 107 | + expect(connection.sendToParent).toHaveBeenCalledWith( |
| 108 | + "updateAssetSidebarWidth", |
| 109 | + mockWidth |
| 110 | + ); |
| 111 | + expect(result).toEqual(undefined); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + describe("replaceAsset", () => { |
| 116 | + it("should sync the upstream asset with the current", async () => { |
| 117 | + const mockHandleUpload = jest.fn(); |
| 118 | + (Asset as jest.Mock).mockReturnValue({ |
| 119 | + handleUpload: mockHandleUpload, |
| 120 | + }); |
| 121 | + const file = {} as any; |
| 122 | + const result = await assetSidebarWidget.replaceAsset(file); |
| 123 | + expect(Asset).toHaveBeenLastCalledWith(emitter); |
| 124 | + expect(mockHandleUpload).toHaveBeenCalledWith([file], "replace"); |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + describe("onSave", () => { |
| 129 | + it("should only accept functions as a callback", () => { |
| 130 | + const mockCallback: any = {}; |
| 131 | + const error = new Error("Callback must be a function"); |
| 132 | + expect(() => assetSidebarWidget.onSave(mockCallback)).toThrow( |
| 133 | + error |
| 134 | + ); |
| 135 | + }); |
| 136 | + |
| 137 | + it("should setup a listener for assetSave event", () => { |
| 138 | + const mockCallback = jest.fn(); |
| 139 | + const result = assetSidebarWidget.onSave(mockCallback); |
| 140 | + expect(emitter.on).toHaveBeenLastCalledWith( |
| 141 | + "assetSave", |
| 142 | + expect.any(Function) |
| 143 | + ); |
| 144 | + expect(result).toBeUndefined(); |
| 145 | + }); |
| 146 | + |
| 147 | + it("should invoke the callback provided ", () => { |
| 148 | + const mockCallback = jest.fn(); |
| 149 | + jest.useFakeTimers(); |
| 150 | + assetSidebarWidget.onSave(mockCallback); |
| 151 | + expect(emitter.on).toHaveBeenLastCalledWith( |
| 152 | + "assetSave", |
| 153 | + expect.any(Function) |
| 154 | + ); |
| 155 | + jest.runAllTimers(); |
| 156 | + expect(mockCallback).toHaveBeenCalled(); |
| 157 | + }); |
| 158 | + }); |
| 159 | + |
| 160 | + describe("onChange", () => { |
| 161 | + it("should only accept functions as a callback", () => { |
| 162 | + const mockCallback: any = {}; |
| 163 | + const error = new Error("Callback must be a function"); |
| 164 | + expect(() => assetSidebarWidget.onChange(mockCallback)).toThrow( |
| 165 | + error |
| 166 | + ); |
| 167 | + }); |
| 168 | + |
| 169 | + it("should setup a listener for assetSave event", () => { |
| 170 | + const mockCallback = jest.fn(); |
| 171 | + const result = assetSidebarWidget.onChange(mockCallback); |
| 172 | + expect(emitter.on).toHaveBeenLastCalledWith( |
| 173 | + "assetChange", |
| 174 | + expect.any(Function) |
| 175 | + ); |
| 176 | + expect(result).toBeUndefined(); |
| 177 | + }); |
| 178 | + |
| 179 | + it("should invoke the callback provided ", () => { |
| 180 | + const mockCallback = jest.fn(); |
| 181 | + jest.useFakeTimers(); |
| 182 | + assetSidebarWidget.onChange(mockCallback); |
| 183 | + expect(emitter.on).toHaveBeenLastCalledWith( |
| 184 | + "assetChange", |
| 185 | + expect.any(Function) |
| 186 | + ); |
| 187 | + jest.runAllTimers(); |
| 188 | + expect(mockCallback).toHaveBeenCalled(); |
| 189 | + }); |
| 190 | + }); |
| 191 | + |
| 192 | + describe("onPublish", () => { |
| 193 | + it("should only accept functions as a callback", () => { |
| 194 | + const mockCallback: any = {}; |
| 195 | + const error = new Error("Callback must be a function"); |
| 196 | + expect(() => assetSidebarWidget.onPublish(mockCallback)).toThrow( |
| 197 | + error |
| 198 | + ); |
| 199 | + }); |
| 200 | + |
| 201 | + it("should setup a listener for assetSave event", () => { |
| 202 | + const mockCallback = jest.fn(); |
| 203 | + const result = assetSidebarWidget.onPublish(mockCallback); |
| 204 | + expect(emitter.on).toHaveBeenLastCalledWith( |
| 205 | + "assetPublish", |
| 206 | + expect.any(Function) |
| 207 | + ); |
| 208 | + expect(result).toBeUndefined(); |
| 209 | + }); |
| 210 | + |
| 211 | + it("should invoke the callback provided ", () => { |
| 212 | + const mockCallback = jest.fn(); |
| 213 | + jest.useFakeTimers(); |
| 214 | + assetSidebarWidget.onPublish(mockCallback); |
| 215 | + expect(emitter.on).toHaveBeenLastCalledWith( |
| 216 | + "assetPublish", |
| 217 | + expect.any(Function) |
| 218 | + ); |
| 219 | + jest.runAllTimers(); |
| 220 | + expect(mockCallback).toHaveBeenCalled(); |
| 221 | + }); |
| 222 | + }); |
| 223 | + |
| 224 | + describe("onUnPublish", () => { |
| 225 | + it("should only accept functions as a callback", () => { |
| 226 | + const mockCallback: any = {}; |
| 227 | + const error = new Error("Callback must be a function"); |
| 228 | + expect(() => assetSidebarWidget.onUnPublish(mockCallback)).toThrow( |
| 229 | + error |
| 230 | + ); |
| 231 | + }); |
| 232 | + |
| 233 | + it("should setup a listener for assetSave event", () => { |
| 234 | + const mockCallback = jest.fn(); |
| 235 | + const result = assetSidebarWidget.onUnPublish(mockCallback); |
| 236 | + expect(emitter.on).toHaveBeenLastCalledWith( |
| 237 | + "assetUnPublish", |
| 238 | + expect.any(Function) |
| 239 | + ); |
| 240 | + expect(result).toBeUndefined(); |
| 241 | + }); |
| 242 | + |
| 243 | + it("should invoke the callback provided ", () => { |
| 244 | + const mockCallback = jest.fn(); |
| 245 | + jest.useFakeTimers(); |
| 246 | + assetSidebarWidget.onUnPublish(mockCallback); |
| 247 | + expect(emitter.on).toHaveBeenLastCalledWith( |
| 248 | + "assetUnPublish", |
| 249 | + expect.any(Function) |
| 250 | + ); |
| 251 | + jest.runAllTimers(); |
| 252 | + expect(mockCallback).toHaveBeenCalled(); |
| 253 | + }); |
| 254 | + }); |
| 255 | +}); |
0 commit comments