|
| 1 | +import { org } from "@modelix/model-client"; |
| 2 | +import { INodeJS, toRoleJS } from "@modelix/ts-model-api"; |
| 3 | +import { watchEffect, type Ref, ref } from "vue"; |
| 4 | +import { useModelClient } from "./useModelClient"; |
| 5 | +import { useReadonlyVersion } from "./useReadonlyVersion"; |
| 6 | +const { loadModelsFromJson } = org.modelix.model.client2; |
| 7 | + |
| 8 | +type VersionInformationWithModelTree = |
| 9 | + org.modelix.model.client2.VersionInformationWithModelTree; |
| 10 | +type MutableModelTreeJs = org.modelix.model.client2.MutableModelTreeJs; |
| 11 | +type ClientJS = org.modelix.model.client2.ClientJS; |
| 12 | +type VersionInformationJS = org.modelix.model.client2.VersionInformationJS; |
| 13 | + |
| 14 | +class SuccessfulVersionInformationWithModelTree { |
| 15 | + public version: VersionInformationJS; |
| 16 | + public tree: MutableModelTreeJs; |
| 17 | + |
| 18 | + constructor(versionHash: string) { |
| 19 | + const modelData = { |
| 20 | + root: {}, |
| 21 | + }; |
| 22 | + |
| 23 | + const rootNode = loadModelsFromJson([JSON.stringify(modelData)]); |
| 24 | + rootNode.setPropertyValue(toRoleJS("versionHash"), versionHash); |
| 25 | + this.tree = { |
| 26 | + rootNode, |
| 27 | + addListener: jest.fn(), |
| 28 | + } as unknown as MutableModelTreeJs; |
| 29 | + this.version = { |
| 30 | + author: "basti", |
| 31 | + time: new Date(), |
| 32 | + versionHash: "SQnQb*ZS1Vi8Mss2HAu0ENbcpstmqb5E6TuZvpSH1TW4", |
| 33 | + } as unknown as VersionInformationJS; |
| 34 | + } |
| 35 | + |
| 36 | + addListener = jest.fn(); |
| 37 | +} |
| 38 | + |
| 39 | +test("test version is loaded", (done) => { |
| 40 | + class SuccessfulClientJS { |
| 41 | + loadReadonlyVersion( |
| 42 | + _repositoryId: string, |
| 43 | + _versionHash: string, |
| 44 | + ): Promise<VersionInformationWithModelTree> { |
| 45 | + return Promise.resolve( |
| 46 | + new SuccessfulVersionInformationWithModelTree( |
| 47 | + _versionHash, |
| 48 | + ) as unknown as VersionInformationWithModelTree, |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + dispose = jest.fn(); |
| 53 | + } |
| 54 | + |
| 55 | + const { client } = useModelClient("anURL", () => |
| 56 | + Promise.resolve(new SuccessfulClientJS() as unknown as ClientJS), |
| 57 | + ); |
| 58 | + const { rootNode, versionInformation } = useReadonlyVersion( |
| 59 | + client, |
| 60 | + "aRepository", |
| 61 | + "aVersionHash", |
| 62 | + ); |
| 63 | + watchEffect(() => { |
| 64 | + if (rootNode.value !== null && versionInformation.value !== null) { |
| 65 | + expect(rootNode.value.getPropertyValue(toRoleJS("versionHash"))).toBe( |
| 66 | + "aVersionHash", |
| 67 | + ); |
| 68 | + done(); |
| 69 | + } |
| 70 | + }); |
| 71 | +}); |
| 72 | + |
| 73 | +test("test connection error is exposed", (done) => { |
| 74 | + class FailingClientJS { |
| 75 | + loadReadonlyVersion( |
| 76 | + _repositoryId: string, |
| 77 | + _versionHash: string, |
| 78 | + ): Promise<VersionInformationWithModelTree> { |
| 79 | + return Promise.reject("Could not connect."); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + const { client } = useModelClient("anURL", () => |
| 84 | + Promise.resolve(new FailingClientJS() as unknown as ClientJS), |
| 85 | + ); |
| 86 | + |
| 87 | + const { error } = useReadonlyVersion(client, "aRepository", "aVersionHash"); |
| 88 | + |
| 89 | + watchEffect(() => { |
| 90 | + if (error.value !== null) { |
| 91 | + expect(error.value).toBe("Could not connect."); |
| 92 | + done(); |
| 93 | + } |
| 94 | + }); |
| 95 | +}); |
| 96 | + |
| 97 | +describe("does not try loading", () => { |
| 98 | + const loadReadonlyVersion = jest.fn((repositoryId, versionHash) => |
| 99 | + Promise.resolve( |
| 100 | + new SuccessfulVersionInformationWithModelTree( |
| 101 | + versionHash, |
| 102 | + ) as unknown as VersionInformationWithModelTree, |
| 103 | + ), |
| 104 | + ); |
| 105 | + |
| 106 | + let client: Ref<ClientJS | null>; |
| 107 | + class MockClientJS { |
| 108 | + loadReadonlyVersion( |
| 109 | + _repositoryId: string, |
| 110 | + _versionHash: string, |
| 111 | + ): Promise<VersionInformationWithModelTree> { |
| 112 | + return loadReadonlyVersion(_repositoryId, _versionHash); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + beforeEach(() => { |
| 117 | + jest.clearAllMocks(); |
| 118 | + client = useModelClient("anURL", () => |
| 119 | + Promise.resolve(new MockClientJS() as unknown as ClientJS), |
| 120 | + ).client; |
| 121 | + }); |
| 122 | + |
| 123 | + test("if client is undefined", () => { |
| 124 | + useReadonlyVersion(undefined, "aRepository", "aVersionHash"); |
| 125 | + expect(loadReadonlyVersion).not.toHaveBeenCalled(); |
| 126 | + }); |
| 127 | + |
| 128 | + test("if repositoryId is undefined", () => { |
| 129 | + useReadonlyVersion(client, undefined, "aVersionHash"); |
| 130 | + expect(loadReadonlyVersion).not.toHaveBeenCalled(); |
| 131 | + }); |
| 132 | + |
| 133 | + test("if branchId is undefined", () => { |
| 134 | + useReadonlyVersion(client, "aRepository", undefined); |
| 135 | + expect(loadReadonlyVersion).not.toHaveBeenCalled(); |
| 136 | + }); |
| 137 | +}); |
0 commit comments