Skip to content

Commit dec41e5

Browse files
Add tests for DialogueGroup component (#107)
1 parent 4de90ba commit dec41e5

File tree

5 files changed

+199
-15
lines changed

5 files changed

+199
-15
lines changed
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
--!strict
2+
3+
local root = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent;
4+
local DialogueGroup = require(script.Parent);
5+
local VirtualService = require(root.VirtualService);
6+
local React = require(root.roblox_packages.react);
7+
local ReactRoblox = require(root.roblox_packages["react-roblox"]);
8+
local IJW = require(root.roblox_packages.ijw);
9+
local expect = IJW.expect;
10+
local describe = IJW.describe;
11+
local it = IJW.it;
12+
13+
local screenGui: ScreenGui?;
14+
local reactRoot: ReactRoblox.RootType?;
15+
16+
return {
17+
describe("DialogueGroup", function()
18+
19+
local function MockComponent(properties: any)
20+
21+
local dialogueType = properties.type or "Message";
22+
local onRendered = properties.onRendered;
23+
local dialogueScripts = properties.dialogueScripts or {};
24+
local layoutOrder = properties.layoutOrder or 1;
25+
26+
React.useEffect(function()
27+
28+
if onRendered then
29+
30+
onRendered();
31+
32+
end;
33+
34+
end, {});
35+
36+
return React.createElement(DialogueGroup, {
37+
name = dialogueType;
38+
plugin = VirtualService.mocks.globals.plugin;
39+
dialogueScripts = dialogueScripts;
40+
layoutOrder = layoutOrder;
41+
});
42+
43+
end;
44+
45+
return {
46+
47+
it("creates a DialogueItem component for each child dialogue of the selected script", function()
48+
49+
expect(function()
50+
51+
-- Render the component and wait for it to finish rendering.
52+
assert(screenGui, "ScreenGui should be initialized before running tests.");
53+
assert(reactRoot, "React root should be initialized before running tests.");
54+
55+
local renderedEvent = Instance.new("BindableEvent");
56+
local messages = {"This should be a message", "This should be another message", "This should be a third message", "This should be a fourth message"};
57+
local dialogueScripts = {};
58+
for _, message in messages do
59+
60+
local dialogueScript = Instance.new("ModuleScript");
61+
dialogueScript.Name = tostring(#dialogueScripts + 1);
62+
dialogueScript:SetAttribute("DialogueContent", message);
63+
table.insert(dialogueScripts, dialogueScript);
64+
65+
end;
66+
67+
local element = React.createElement(MockComponent, {
68+
dialogueScripts = dialogueScripts;
69+
onRendered = function()
70+
71+
renderedEvent:Fire();
72+
73+
end;
74+
});
75+
76+
reactRoot:render(element);
77+
renderedEvent.Event:Wait();
78+
79+
-- Simulate a click on the component.
80+
local dialogueGroup = screenGui:FindFirstChildOfClass("Frame");
81+
assert(dialogueGroup, "DialogueGroup should be rendered in the ScreenGui.");
82+
83+
local dialogueItems = {};
84+
for _, child in dialogueGroup:GetChildren() do
85+
86+
if child:IsA("Frame") then
87+
88+
table.insert(dialogueItems, child);
89+
90+
local viewButton = child:FindFirstChild("ViewButton");
91+
assert(viewButton and viewButton:IsA("GuiButton"), "ViewButton should be present in the DialogueItem.");
92+
93+
local informationFrame = viewButton:FindFirstChild("InformationFrame");
94+
assert(informationFrame, "Information should be present in the ViewButton.");
95+
96+
local descriptionLabel = informationFrame:FindFirstChild("DescriptionLabel");
97+
assert(descriptionLabel and descriptionLabel:IsA("TextLabel"), "DescriptionLabel should be present in the InformationFrame.");
98+
99+
local expectedMessage = messages[child.LayoutOrder];
100+
expect(descriptionLabel.Text).toBe(expectedMessage);
101+
102+
end;
103+
104+
end;
105+
106+
expect(#dialogueItems).toBe(4);
107+
108+
end).toFinishBeforeSeconds(1);
109+
110+
end);
111+
112+
}
113+
114+
end, {
115+
beforeEach = function()
116+
117+
VirtualService.mocks.isEnabled = true;
118+
119+
local newScreenGui = Instance.new("ScreenGui");
120+
screenGui = newScreenGui;
121+
reactRoot = ReactRoblox.createRoot(newScreenGui);
122+
123+
end;
124+
afterEach = function()
125+
126+
VirtualService.mocks.isEnabled = false;
127+
128+
if reactRoot then
129+
130+
reactRoot:unmount();
131+
132+
end;
133+
134+
if screenGui then
135+
136+
screenGui:Destroy();
137+
138+
end;
139+
140+
end;
141+
})
142+
};

src/DialogueEditor/components/Explorer/components/DialogueGroupContainer/components/DialogueGroup/components/DialogueItem/DialogueItem.test.luau

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,51 @@ return {
237237
end).toFinishBeforeSeconds(1);
238238

239239
end);
240+
241+
it("can preview static dialogue content string", function()
242+
243+
expect(function()
244+
245+
-- Render the component and wait for it to finish rendering.
246+
assert(screenGui, "ScreenGui should be initialized before running tests.");
247+
assert(reactRoot, "React root should be initialized before running tests.");
248+
249+
local expectedMessage = "This is a static dialogue content string.";
250+
local renderedEvent = Instance.new("BindableEvent");
251+
local dialogueScript = Instance.new("ModuleScript");
252+
dialogueScript.Name = "1";
253+
dialogueScript:SetAttribute("DialogueContent", expectedMessage);
254+
255+
local element = React.createElement(MockComponent, {
256+
dialogueScript = dialogueScript;
257+
onRendered = function()
258+
259+
renderedEvent:Fire();
260+
261+
end;
262+
});
263+
264+
reactRoot:render(element);
265+
renderedEvent.Event:Wait();
266+
267+
-- Simulate a click on the component.
268+
local dialogueItem = screenGui:FindFirstChildOfClass("Frame");
269+
assert(dialogueItem, "DialogueItem should be rendered in the ScreenGui.");
270+
271+
local viewButton = dialogueItem:FindFirstChild("ViewButton");
272+
assert(viewButton and viewButton:IsA("GuiButton"), "ViewButton should be present in the DialogueItem.");
273+
274+
local informationFrame = viewButton:FindFirstChild("InformationFrame");
275+
assert(informationFrame, "Information should be present in the ViewButton.");
276+
277+
local descriptionLabel = informationFrame:FindFirstChild("DescriptionLabel");
278+
assert(descriptionLabel and descriptionLabel:IsA("TextLabel"), "DescriptionLabel should be present in the InformationFrame.");
279+
expect(descriptionLabel.Text).toBe(expectedMessage);
280+
281+
end).toFinishBeforeSeconds(1);
282+
283+
end);
284+
240285
}
241286

242287
end, {

src/DialogueEditor/components/Explorer/components/DialogueGroupContainer/components/DialogueGroup/components/DialogueItem/init.luau

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ export type DialogueItemProperties = {
1616
dialogueScript: ModuleScript;
1717
dialogueScriptCount: number;
1818
layoutOrder: number;
19-
setSettingsTarget: (target: ModuleScript?) -> ();
2019
}
2120

2221
local function DialogueItem(props: DialogueItemProperties)
@@ -210,7 +209,7 @@ local function DialogueItem(props: DialogueItemProperties)
210209
PaddingTop = UDim.new(0, 15);
211210
PaddingBottom = UDim.new(0, 15);
212211
});
213-
Information = React.createElement("Frame", {
212+
InformationFrame = React.createElement("Frame", {
214213
AutomaticSize = Enum.AutomaticSize.XY;
215214
LayoutOrder = 1;
216215
BackgroundTransparency = 1;
@@ -232,7 +231,7 @@ local function DialogueItem(props: DialogueItemProperties)
232231
ImageColor3 = colors.text;
233232
ImageTransparency = if isDeprioritized then 0.5 else 0;
234233
});
235-
Description = React.createElement("TextLabel", {
234+
DescriptionLabel = React.createElement("TextLabel", {
236235
Text = if dialogueType == "Conversation" then dialogueScript.Name else (dialogueContent or "Nothing yet...");
237236
TextColor3 = colors.text;
238237
TextTransparency = if not isDeprioritized and (dialogueType == "Conversation" or dialogueContent) then 0 else 0.5;

src/DialogueEditor/components/Explorer/components/DialogueGroupContainer/components/DialogueGroup/init.luau

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,34 @@
33
local root = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent;
44
local React = require(root.roblox_packages.react);
55
local DialogueItem = require(script.components.DialogueItem);
6+
local VirtualService = require(root.VirtualService);
67

78
export type DialogueItemType = DialogueItem.DialogueItemType;
89

910
export type DialogueGroupProperties = {
1011
name: DialogueItemType;
1112
plugin: Plugin;
12-
scriptList: {ModuleScript};
13+
dialogueScripts: {ModuleScript};
1314
layoutOrder: number;
14-
setSettingsTarget: (target: ModuleScript?) -> ();
1515
}
1616

17-
local function SettingGroup(properties: DialogueGroupProperties)
17+
local function DialogueGroup(properties: DialogueGroupProperties)
1818

1919
local groupName = properties.name;
20-
local scriptList = properties.scriptList;
20+
local dialogueScripts = properties.dialogueScripts;
2121
local layoutOrder = properties.layoutOrder;
22-
local setSettingsTarget = properties.setSettingsTarget;
22+
local plugin = if VirtualService.mocks.isEnabled then VirtualService.mocks.globals.plugin else properties.plugin;
2323

2424
local scriptComponents = {};
25-
for index, dialogueScript in scriptList do
25+
for index, dialogueScript in dialogueScripts do
2626

2727
local settingComponent = React.createElement(DialogueItem, {
2828
key = dialogueScript:GetFullName();
29-
plugin = properties.plugin;
29+
plugin = plugin;
3030
dialogueScript = dialogueScript;
31-
dialogueScriptCount = #scriptList;
31+
dialogueScriptCount = #dialogueScripts;
3232
layoutOrder = index;
3333
type = groupName :: DialogueItemType;
34-
setSettingsTarget = setSettingsTarget;
3534
});
3635

3736
table.insert(scriptComponents, settingComponent);
@@ -53,4 +52,4 @@ local function SettingGroup(properties: DialogueGroupProperties)
5352

5453
end;
5554

56-
return React.memo(SettingGroup);
55+
return React.memo(DialogueGroup);

src/DialogueEditor/components/Explorer/components/DialogueGroupContainer/init.luau

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,7 @@ local function DialogueGroupContainer(props: DialogueTableBodyProperties)
202202
layoutOrder = categoryIndex;
203203
plugin = props.plugin;
204204
key = dialogueType;
205-
scriptList = scriptList;
206-
setSettingsTarget = setSettingsTarget;
205+
dialogueScripts = scriptList;
207206
});
208207

209208
table.insert(dialogueGroups, dialogueGroup);

0 commit comments

Comments
 (0)