-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlive-preview.spec.ts
More file actions
193 lines (183 loc) · 6.64 KB
/
live-preview.spec.ts
File metadata and controls
193 lines (183 loc) · 6.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import * as contentstack from "../../src/lib/contentstack";
import { TEntry } from "./types";
import dotenv from "dotenv";
dotenv.config();
const apiKey = process.env.API_KEY as string;
const deliveryToken = process.env.DELIVERY_TOKEN as string;
const environment = process.env.ENVIRONMENT as string;
const branch = process.env.BRANCH as string;
const entryUid = process.env.ENTRY_UID as string;
const previewToken = process.env.PREVIEW_TOKEN as string;
const managementToken = process.env.MANAGEMENT_TOKEN as string;
const host = process.env.HOST as string;
describe("Live preview tests", () => {
test("should check for values initialized", () => {
const stack = contentstack.stack({
apiKey: apiKey,
deliveryToken: deliveryToken,
environment: environment,
branch: branch,
});
const livePreviewObject = stack.config.live_preview;
expect(livePreviewObject).toBeUndefined();
expect(stack.config.host).toBe("cdn.contentstack.io");
expect(stack.config.branch).toBe(branch);
});
test("should check host when live preview is enabled and management token is provided", () => {
const stack = contentstack.stack({
apiKey: apiKey,
deliveryToken: deliveryToken,
environment: environment,
live_preview: {
enable: true,
management_token: managementToken,
host: host,
},
});
const livePreviewObject = stack.config.live_preview;
expect(livePreviewObject).not.toBeUndefined();
expect(livePreviewObject).toHaveProperty("enable");
expect(livePreviewObject).toHaveProperty("host");
expect(livePreviewObject).not.toHaveProperty("preview");
expect(stack.config.host).toBe("cdn.contentstack.io");
});
test("should check host when live preview is disabled and management token is provided", () => {
const stack = contentstack.stack({
apiKey: apiKey,
deliveryToken: deliveryToken,
environment: environment,
live_preview: {
enable: false,
management_token: managementToken,
},
});
const livePreviewObject = stack.config.live_preview;
expect(livePreviewObject).not.toBeUndefined();
expect(livePreviewObject).toHaveProperty("enable");
expect(livePreviewObject).not.toHaveProperty("host");
expect(livePreviewObject).not.toHaveProperty("preview");
expect(stack.config.host).toBe("cdn.contentstack.io");
});
test("should check host when live preview is enabled and preview token is provided", () => {
const stack = contentstack.stack({
apiKey: apiKey,
deliveryToken: deliveryToken,
environment: environment,
live_preview: {
enable: true,
preview_token: previewToken,
host: host,
},
});
const livePreviewObject = stack.config.live_preview;
expect(livePreviewObject).not.toBeUndefined();
expect(livePreviewObject).toHaveProperty("enable");
expect(livePreviewObject).toHaveProperty("host");
expect(livePreviewObject).not.toHaveProperty("preview");
expect(stack.config.host).toBe("cdn.contentstack.io");
});
test("should check host when live preview is disabled and preview token is provided", () => {
const stack = contentstack.stack({
apiKey: apiKey,
deliveryToken: deliveryToken,
environment: environment,
live_preview: {
enable: false,
preview_token: previewToken,
},
});
const livePreviewObject = stack.config.live_preview;
expect(livePreviewObject).not.toBeUndefined();
expect(livePreviewObject).toHaveProperty("enable");
expect(livePreviewObject).not.toHaveProperty("host");
expect(livePreviewObject).not.toHaveProperty("preview");
expect(stack.config.host).toBe("cdn.contentstack.io");
});
});
describe("Live preview query Entry API tests", () => {
it("should check for entry when live preview is enabled with management token", async () => {
try {
const stack = contentstack.stack({
apiKey: process.env.API_KEY as string,
deliveryToken: process.env.DELIVERY_TOKEN as string,
environment: process.env.ENVIRONMENT as string,
live_preview: {
enable: true,
management_token: managementToken,
host: host,
},
});
stack.livePreviewQuery({
contentTypeUid: "blog_post",
live_preview: "ser",
});
const result = await stack
.contentType("blog_post")
.entry(entryUid)
.fetch<TEntry>();
expect(result).toBeDefined();
expect(result._version).toBeDefined();
expect(result.locale).toEqual("en-us");
expect(result.uid).toBeDefined();
expect(result.created_by).toBeDefined();
expect(result.updated_by).toBeDefined();
} catch (error: any) {
expect(error).toBeDefined();
// AxiosError: error.response contains the response object
expect(error.response).toBeDefined();
expect(error.response.status).toEqual(403);
}
});
it("should check for entry is when live preview is disabled with management token", async () => {
const stack = contentstack.stack({
host: process.env.HOST as string,
apiKey: process.env.API_KEY as string,
deliveryToken: process.env.DELIVERY_TOKEN as string,
environment: process.env.ENVIRONMENT as string,
live_preview: {
enable: false,
management_token: managementToken,
},
});
stack.livePreviewQuery({
contentTypeUid: "blog_post",
live_preview: "ser",
});
const result = await stack
.contentType("blog_post")
.entry(entryUid)
.fetch<TEntry>();
expect(result).toBeDefined();
expect(result._version).toBeDefined();
expect(result.locale).toEqual("en-us");
expect(result.uid).toBeDefined();
expect(result.created_by).toBeDefined();
expect(result.updated_by).toBeDefined();
});
it("should check for entry is when live preview is disabled with preview token", async () => {
const stack = contentstack.stack({
host: process.env.HOST as string,
apiKey: process.env.API_KEY as string,
deliveryToken: process.env.DELIVERY_TOKEN as string,
environment: process.env.ENVIRONMENT as string,
live_preview: {
enable: false,
preview_token: previewToken,
},
});
stack.livePreviewQuery({
contentTypeUid: "blog_post",
live_preview: "ser",
});
const result = await stack
.contentType("blog_post")
.entry(entryUid)
.fetch<TEntry>();
expect(result).toBeDefined();
expect(result._version).toBeDefined();
expect(result.locale).toEqual("en-us");
expect(result.uid).toBeDefined();
expect(result.created_by).toBeDefined();
expect(result.updated_by).toBeDefined();
});
});