Skip to content

Commit 4c11b72

Browse files
committed
fix error
1 parent 27b8616 commit 4c11b72

1 file changed

Lines changed: 122 additions & 0 deletions

File tree

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
using System;
2+
using Xunit;
3+
using Contentstack.Core.Configuration;
4+
using System.Threading.Tasks;
5+
using System.Collections.Generic;
6+
using Newtonsoft.Json.Linq;
7+
using Newtonsoft.Json;
8+
9+
namespace Contentstack.Core.Tests
10+
{
11+
public class TestContentstackClient : ContentstackClient
12+
{
13+
public TestContentstackClient(ContentstackOptions options)
14+
: base(options)
15+
{
16+
}
17+
18+
// Override GetLivePreviewData with a hardcoded response
19+
private new async Task<JObject> GetLivePreviewData()
20+
{
21+
var mockResponse = new
22+
{
23+
entry = new
24+
{
25+
uid = "mock_entry_uid",
26+
title = "Mocked Entry",
27+
content_type_uid = "mock_content_type",
28+
status = "preview"
29+
}
30+
};
31+
string jsonResponse = Newtonsoft.Json.JsonConvert.SerializeObject(mockResponse);
32+
JObject data = JsonConvert.DeserializeObject<JObject>(jsonResponse, this.SerializerSettings);
33+
return await Task.FromResult((JObject)data["entry"]);
34+
}
35+
36+
// Public method to access the private method in tests
37+
public async Task<JObject> TestGetLivePreviewData()
38+
{
39+
return await GetLivePreviewData();
40+
}
41+
}
42+
43+
public class LivePreviewTests
44+
{
45+
ContentstackClient client = StackConfig.GetStack();
46+
47+
ContentstackClient Lpclient = StackConfig.GetLPStack();
48+
49+
private String numbersContentType = "numbers_content_type";
50+
String source = "source";
51+
52+
public double EPSILON { get; private set; }
53+
54+
[Fact]
55+
public async Task CheckLivePreviewConfigNotSet()
56+
{
57+
var LPConfig = client.GetLivePreviewConfig();
58+
Assert.False(LPConfig.Enable);
59+
Assert.Null(LPConfig.PreviewToken);
60+
Assert.Null(LPConfig.Host);
61+
}
62+
63+
[Fact]
64+
public async Task CheckLivePreviewConfigSet()
65+
{
66+
var LPConfig = Lpclient.GetLivePreviewConfig();
67+
Assert.True(LPConfig.Enable);
68+
Assert.NotEmpty(LPConfig.PreviewToken);
69+
Assert.NotEmpty(LPConfig.Host);
70+
}
71+
72+
[Fact]
73+
public async Task setQueryWithLivePreview()
74+
{
75+
Dictionary<string, string> query = new Dictionary<string, string>
76+
{
77+
{ "content_type_uid", "ct1" },
78+
{ "live_preview", "lphash" },
79+
{ "release_id", "release_id" },
80+
{ "preview_timestamp", "preview_timestamp" },
81+
{ "entry_uid", "euid" }
82+
};
83+
Lpclient.LivePreviewQueryAsync(query);
84+
var LPConfig = Lpclient.GetLivePreviewConfig();
85+
Assert.Equal(LPConfig.previewTimestamp, "preview_timestamp");
86+
Assert.NotEmpty(LPConfig.PreviewToken);
87+
Assert.NotEmpty(LPConfig.PreviewToken);
88+
Assert.NotEmpty(LPConfig.Host);
89+
}
90+
91+
[Fact]
92+
public async Task TestGetLivePreviewData()
93+
{
94+
// Arrange
95+
var options = new ContentstackOptions
96+
{
97+
ApiKey = "test_api_key",
98+
DeliveryToken = "test_delivery_token",
99+
Environment = "test_environment",
100+
LivePreview = new LivePreviewConfig
101+
{
102+
Enable = true,
103+
PreviewToken = "preview_token", // Replace with a valid preview token
104+
Host = "test-host" // Replace with a valid preview host (e.g., "rest-preview.contentstack.com")
105+
106+
}
107+
};
108+
109+
var client = new TestContentstackClient(options);
110+
111+
// Act
112+
var result = await client.TestGetLivePreviewData();
113+
114+
// Assert
115+
Assert.NotNull(result);
116+
Assert.Equal("mock_entry_uid", result["uid"].ToString());
117+
Assert.Equal("Mocked Entry", result["title"].ToString());
118+
}
119+
120+
}
121+
}
122+

0 commit comments

Comments
 (0)