github.com/tilt-dev/tilt@v0.33.15-0.20240515162809-0a22ed45d8a0/web/src/AppController.test.ts (about) 1 import fetchMock from "fetch-mock" 2 import AppController from "./AppController" 3 import PathBuilder from "./PathBuilder" 4 5 let fakeSetHistoryLocation = jest.fn() 6 let fakeOnAppChange = jest.fn() 7 8 const HUD = { 9 onAppChange: fakeOnAppChange, 10 setHistoryLocation: fakeSetHistoryLocation, 11 } 12 13 function flushPromises() { 14 return new Promise((resolve) => setTimeout(resolve, 0)) 15 } 16 17 describe("AppController", () => { 18 beforeEach(() => { 19 fetchMock.reset() 20 fakeSetHistoryLocation.mockReset() 21 fakeSetHistoryLocation.mockReset() 22 }) 23 24 it("sets view from snapshot", async () => { 25 fetchMock.mock( 26 "/api/snapshot/aaaaaaa", 27 JSON.stringify({ view: { uiResources: [] } }) 28 ) 29 30 let pb = PathBuilder.forTesting("cloud.tilt.dev", "/snapshot/aaaaaaa") 31 let ac = new AppController(pb, HUD) 32 ac.setStateFromSnapshot() 33 34 await flushPromises() 35 expect(fakeOnAppChange.mock.calls.length).toBe(1) 36 expect(fakeSetHistoryLocation.mock.calls.length).toBe(0) 37 }) 38 39 it("sets view and path from snapshot", async () => { 40 fetchMock.mock( 41 "/api/snapshot/aaaaaa", 42 JSON.stringify({ view: { uiResources: [] }, path: "/foo" }) 43 ) 44 45 let pb = PathBuilder.forTesting("cloud.tilt.dev", "/snapshot/aaaaaa") 46 let ac = new AppController(pb, HUD) 47 ac.setStateFromSnapshot() 48 49 await flushPromises() 50 expect(fakeOnAppChange.mock.calls.length).toBe(1) 51 expect(fakeSetHistoryLocation.mock.calls.length).toBe(1) 52 expect(fakeSetHistoryLocation.mock.calls[0][0]).toBe("/snapshot/aaaaaa/foo") 53 }) 54 })