Skip to content

Commit 5d9466e

Browse files
Add test: AutoTriggerCheckbox can add "ShouldAutoTriggerConversation" tag to selected script
1 parent 00626a6 commit 5d9466e

File tree

2 files changed

+177
-6
lines changed

2 files changed

+177
-6
lines changed

src/DialogueEditor/components/Checkbox/init.luau

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ local root = script.Parent.Parent.Parent;
44
local React = require(root.roblox_packages.react);
55
local useStudioColors = require(root.DialogueEditor.hooks.useStudioColors);
66
local useStudioIcons = require(root.DialogueEditor.hooks.useStudioIcons);
7+
local VirtualService = require(root.VirtualService);
78

89
export type CheckboxProperties = {
910
text: string;
@@ -22,6 +23,34 @@ local function Checkbox(properties: CheckboxProperties)
2223
local onChanged = properties.onChanged;
2324
local colors = useStudioColors();
2425
local icons = useStudioIcons();
26+
local buttonRef = React.useRef(nil :: TextButton?);
27+
28+
React.useEffect(function()
29+
30+
local button = buttonRef.current;
31+
assert(button, "Button reference should not be nil.");
32+
33+
local activatedConnection = VirtualService.events.GuiButton.Activated:getSignal(button):Connect(function()
34+
35+
if not isDisabled then
36+
37+
onChanged(not isChecked);
38+
39+
end;
40+
41+
end);
42+
43+
return function()
44+
45+
if activatedConnection then
46+
47+
activatedConnection:Disconnect();
48+
49+
end;
50+
51+
end;
52+
53+
end, {onChanged});
2554

2655
return React.createElement("Frame", {
2756
AutomaticSize = Enum.AutomaticSize.XY;
@@ -41,13 +70,10 @@ local function Checkbox(properties: CheckboxProperties)
4170
BackgroundTransparency = if isDisabled then 0.5 else 0;
4271
LayoutOrder = 1;
4372
Text = "";
44-
[React.Event.Activated] = function()
45-
46-
if not isDisabled then
73+
ref = buttonRef;
74+
[React.Event.Activated] = function(self)
4775

48-
onChanged(not isChecked);
49-
50-
end;
76+
VirtualService.events.GuiButton.Activated:fireEvent(self);
5177

5278
end;
5379
}, {
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
--!strict
2+
3+
local root = script.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent;
4+
local AutoTriggerCheckbox = require(script.Parent);
5+
local VirtualService = require(root.VirtualService);
6+
local React = require(root.roblox_packages.react);
7+
local ReactErrorBoundary = require(root.roblox_packages.ReactErrorBoundary);
8+
local ErrorBoundary = ReactErrorBoundary.ErrorBoundary;
9+
local ReactRoblox = require(root.roblox_packages["react-roblox"]);
10+
local IJW = require(root.roblox_packages.ijw);
11+
local expect = IJW.expect;
12+
local describe = IJW.describe;
13+
local it = IJW.it;
14+
15+
local screenGui: ScreenGui?;
16+
local reactRoot: ReactRoblox.RootType?;
17+
local propagatedErrorMessage;
18+
19+
return {
20+
21+
describe("DialogueGroupContainer", function()
22+
23+
local function MockComponent(properties: any)
24+
25+
local selectedScript = properties.selectedScript;
26+
local onErrored = properties.onErrored or function() end;
27+
local onRendered = properties.onRendered or function() end;
28+
29+
React.useEffect(function()
30+
31+
onRendered();
32+
33+
end, {onRendered});
34+
35+
return React.createElement(ErrorBoundary, {
36+
FallbackComponent = React.Fragment;
37+
onError = onErrored;
38+
}, {
39+
React.createElement(AutoTriggerCheckbox, {
40+
selectedScript = selectedScript;
41+
layoutOrder = 1;
42+
})
43+
});
44+
45+
end;
46+
47+
local function verifyReactStatus()
48+
49+
if propagatedErrorMessage then
50+
51+
error(propagatedErrorMessage);
52+
53+
end;
54+
55+
end;
56+
57+
local function createDialogueScript(name: string)
58+
59+
local dialogueScript = Instance.new("ModuleScript");
60+
dialogueScript:AddTag("DialogueMakerConversationScript");
61+
return dialogueScript;
62+
63+
end;
64+
65+
local function render(): ModuleScript
66+
67+
assert(reactRoot, "React root should be initialized before running tests.");
68+
69+
local selectedScript = createDialogueScript("TestDialogueScript");
70+
local didRender = false;
71+
local element = React.createElement(MockComponent, {
72+
selectedScript = selectedScript;
73+
onErrored = function(errorMessage)
74+
75+
propagatedErrorMessage = errorMessage;
76+
77+
end;
78+
onRendered = function()
79+
80+
didRender = true;
81+
82+
end;
83+
});
84+
85+
reactRoot:render(element);
86+
repeat task.wait() until didRender or propagatedErrorMessage;
87+
88+
return selectedScript;
89+
90+
end;
91+
92+
return {
93+
94+
it(`can add "ShouldAutoTriggerConversation" tag to selected script`, function()
95+
96+
expect(function()
97+
98+
-- Render the component and wait for it to finish rendering.
99+
assert(screenGui, "ScreenGui should be initialized before running tests.");
100+
local selectedScript = render();
101+
verifyReactStatus();
102+
103+
-- Verify that the checkbox is unchecked.
104+
local checkboxFrame = screenGui:FindFirstChildOfClass("Frame");
105+
assert(checkboxFrame, "Frame should be present in the ScreenGui.");
106+
107+
local checkbox = checkboxFrame:FindFirstChild("Checkbox");
108+
assert(checkbox and checkbox:IsA("TextButton"), "Checkbox should be present in the frame.");
109+
110+
VirtualService.events.GuiButton.Activated:fireEvent(checkbox);
111+
expect(selectedScript:GetAttribute("ShouldAutoTriggerConversation")).toBe(true);
112+
113+
end).toFinishBeforeSeconds(1);
114+
115+
end);
116+
117+
}
118+
119+
end, {
120+
beforeEach = function()
121+
122+
local newScreenGui = Instance.new("ScreenGui");
123+
screenGui = newScreenGui;
124+
reactRoot = ReactRoblox.createRoot(newScreenGui);
125+
126+
end;
127+
afterEach = function()
128+
129+
if reactRoot then
130+
131+
reactRoot:unmount();
132+
133+
end;
134+
135+
if screenGui then
136+
137+
screenGui:Destroy();
138+
139+
end;
140+
141+
propagatedErrorMessage = nil;
142+
143+
end;
144+
})
145+
};

0 commit comments

Comments
 (0)