|
1 | | -import Config, { updateConfigFromUrl } from "../configManager"; |
| 1 | +import Config, { updateConfigFromUrl, syncToStackSdk } from "../configManager"; |
2 | 2 | import { getDefaultConfig, getUserInitData } from "../config.default"; |
3 | 3 | import { DeepSignal } from "deepsignal"; |
4 | 4 | import { IConfig } from "../../types/types"; |
@@ -102,6 +102,73 @@ describe("config default flags", () => { |
102 | 102 | }); |
103 | 103 | }); |
104 | 104 |
|
| 105 | +describe("syncToStackSdk", () => { |
| 106 | + beforeEach(() => { |
| 107 | + Config.reset(); |
| 108 | + }); |
| 109 | + |
| 110 | + afterAll(() => { |
| 111 | + Config.reset(); |
| 112 | + }); |
| 113 | + |
| 114 | + test("should set hash, stackSdkLivePreview.hash and stackSdkLivePreview.live_preview when hash is provided", () => { |
| 115 | + syncToStackSdk({ hash: "abc123" }); |
| 116 | + |
| 117 | + const config = Config.get(); |
| 118 | + expect(config.stackSdk.live_preview.hash).toBe("abc123"); |
| 119 | + expect(config.stackSdk.live_preview.live_preview).toBe("abc123"); |
| 120 | + expect(config.stackSdk.live_preview.content_type_uid).toBeUndefined(); |
| 121 | + expect(config.stackSdk.live_preview.entry_uid).toBeUndefined(); |
| 122 | + }); |
| 123 | + |
| 124 | + test("should set content_type_uid on stackSdk when contentTypeUid is provided", () => { |
| 125 | + syncToStackSdk({ contentTypeUid: "blog" }); |
| 126 | + |
| 127 | + const config = Config.get(); |
| 128 | + expect(config.stackSdk.live_preview.content_type_uid).toBe("blog"); |
| 129 | + expect(config.stackSdk.live_preview.hash).toBeUndefined(); |
| 130 | + expect(config.stackSdk.live_preview.entry_uid).toBeUndefined(); |
| 131 | + }); |
| 132 | + |
| 133 | + test("should set entry_uid on stackSdk when entryUid is provided", () => { |
| 134 | + syncToStackSdk({ entryUid: "entry-42" }); |
| 135 | + |
| 136 | + const config = Config.get(); |
| 137 | + expect(config.stackSdk.live_preview.entry_uid).toBe("entry-42"); |
| 138 | + expect(config.stackSdk.live_preview.hash).toBeUndefined(); |
| 139 | + expect(config.stackSdk.live_preview.content_type_uid).toBeUndefined(); |
| 140 | + }); |
| 141 | + |
| 142 | + test("should set all three fields when all params are provided", () => { |
| 143 | + syncToStackSdk({ hash: "h1", contentTypeUid: "page", entryUid: "e1" }); |
| 144 | + |
| 145 | + const config = Config.get(); |
| 146 | + expect(config.stackSdk.live_preview.hash).toBe("h1"); |
| 147 | + expect(config.stackSdk.live_preview.live_preview).toBe("h1"); |
| 148 | + expect(config.stackSdk.live_preview.content_type_uid).toBe("page"); |
| 149 | + expect(config.stackSdk.live_preview.entry_uid).toBe("e1"); |
| 150 | + }); |
| 151 | + |
| 152 | + test("should skip falsy values — null and undefined are ignored", () => { |
| 153 | + syncToStackSdk({ hash: null, contentTypeUid: undefined, entryUid: null }); |
| 154 | + |
| 155 | + const config = Config.get(); |
| 156 | + expect(config.stackSdk.live_preview.hash).toBeUndefined(); |
| 157 | + expect(config.stackSdk.live_preview.content_type_uid).toBeUndefined(); |
| 158 | + expect(config.stackSdk.live_preview.entry_uid).toBeUndefined(); |
| 159 | + }); |
| 160 | + |
| 161 | + test("should not overwrite existing stackSdk values for keys not passed", () => { |
| 162 | + syncToStackSdk({ hash: "first", contentTypeUid: "ct1", entryUid: "e1" }); |
| 163 | + syncToStackSdk({ hash: "second" }); |
| 164 | + |
| 165 | + const config = Config.get(); |
| 166 | + expect(config.stackSdk.live_preview.hash).toBe("second"); |
| 167 | + expect(config.stackSdk.live_preview.content_type_uid).toBe("ct1"); |
| 168 | + expect(config.stackSdk.live_preview.entry_uid).toBe("e1"); |
| 169 | + }); |
| 170 | +}); |
| 171 | + |
105 | 172 | describe("update config from url", () => { |
106 | 173 | let config: DeepSignal<IConfig>; |
107 | 174 |
|
|
0 commit comments