Skip to content

Commit 9f85c24

Browse files
author
RoFlection Bot
committed
port wait-for, test-utils (#32)
1 parent ecc799e commit 9f85c24

File tree

14 files changed

+881
-31
lines changed

14 files changed

+881
-31
lines changed

bin/ci.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ roblox-cli analyze analyze.project.json
1212
stylua -c src types jsHelpers
1313

1414
echo "Run tests"
15-
roblox-cli run --load.place tests.project.json --run bin/spec.lua --lua.globals=__DEV__=true --fastFlags.allOnLuau
15+
roblox-cli run --load.place tests.project.json --run bin/spec.lua --lua.globals=__DEV__=true --fastFlags.allOnLuau --fastFlags.overrides EnableLoadModule=true

bin/spec.lua

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
1-
local ProcessService = game:GetService("ProcessService")
21
local Root = game:GetService("ReplicatedStorage")
32

43
local Packages = Root.Packages
5-
-- Load JestGlobals source into Packages folder so it's next to Roact as expected
6-
local TestEZ = require(Root.Packages.Dev.JestGlobals).TestEZ
4+
5+
local JestGlobals = require(Packages.Dev.JestGlobals)
6+
7+
local TestEZ = JestGlobals.TestEZ
78

89
-- Run all tests, collect results, and report to stdout.
9-
local result = TestEZ.TestBootstrap:run(
10+
TestEZ.TestBootstrap:run(
1011
{ Packages.DomTestingLibrary },
11-
TestEZ.Reporters.TextReporterQuiet
12+
TestEZ.Reporters.pipe({
13+
TestEZ.Reporters.JestDefaultReporter,
14+
TestEZ.Reporters.JestSummaryReporter,
15+
}),
16+
{
17+
extraEnvironment = JestGlobals.testEnv,
18+
}
1219
)
20+
-- ROBLOX TODO: after converting jest-runner this should be included there
21+
JestGlobals.runtime:teardown()
1322

14-
if result.failureCount == 0 and #result.errors == 0 then
15-
ProcessService:ExitAsync(0)
16-
else
17-
ProcessService:ExitAsync(1)
18-
end
23+
return nil

jsHelpers/document.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
local document = Instance.new("Folder")
2+
3+
return document

jsHelpers/isCallable.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
-- ROBLOX upstream: no upstream
2+
3+
-- ROBLOX comment: we use callable tables instead of functions sometimes, so typeof() == "function" isn't enough
4+
-- https://github.com/Roblox/roact-alignment/blob/2e9a4b4b4683da6c603d9c6a9b27b776c5a006c7/modules/react-reconciler/src/ReactFiberDevToolsHook.new.lua#L27-L42
5+
local function isCallable(value: any): boolean
6+
if typeof(value) == "function" then
7+
return true
8+
end
9+
if typeof(value) == "table" then
10+
local mt = getmetatable(value)
11+
if mt and rawget(mt, "__call") then
12+
return true
13+
end
14+
if value._isMockFunction then
15+
return true
16+
end
17+
end
18+
return false
19+
end
20+
21+
return isCallable

rotriever.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ content_root = "src"
99
[dependencies]
1010
LuauPolyfill = "github.com/roblox/luau-polyfill@0.3.4"
1111
LuauRegExp = "github.com/Roblox/luau-regexp@0.2.0"
12+
Promise = "github.com/evaera/roblox-lua-promise@4.0.0"
1213
JestGetType = { git = "https://github.com/roblox/jest-roblox.git", version="2.4.1", package="JestGetType"}
1314
PrettyFormat = { git = "https://github.com/roblox/jest-roblox.git", version="2.4.1", package="PrettyFormat"}
1415
RobloxShared = { git = "https://github.com/roblox/jest-roblox.git", version="2.4.1", package="RobloxShared"}
1516

1617
[dev_dependencies]
17-
JestGlobals = "github.com/roblox/jest-roblox@2.4.1"
18-
Promise = "github.com/evaera/roblox-lua-promise@4.0.0"
18+
JestGlobals = { git = "https://github.com/roblox/jest-roblox.git", rev="master" }

src/__tests__/helpers.spec.lua

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
return function()
33
local Packages = script.Parent.Parent.Parent
44

5-
local Promise = require(Packages.Dev.Promise)
5+
local Promise = require(Packages.Promise)
66

77
local JestGlobals = require(Packages.Dev.JestGlobals)
88
local jestExpect = JestGlobals.expect
99
local jest = JestGlobals.jest
1010

11-
-- ROBLOX deviation START: assume document to be nil
12-
local document = nil
11+
-- ROBLOX deviation START: assume constant document
12+
local document = require(Packages.JsHelpers.document)
1313
-- ROBLOX deviation END
1414

1515
-- ROBLOX TODO START: not ported yet. Mocked

src/__tests__/helpers/test-utils.lua

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
-- ROBLOX upstream: https://github.com/testing-library/dom-testing-library/blob/v8.14.0/src/__tests__/helpers/test-utils.js
2+
-- ROBLOX deviation: wrapping in function to pass afterEach as an argument.
3+
return function(afterEach)
4+
local Packages = script.Parent.Parent.Parent.Parent
5+
local LuauPolyfill = require(Packages.LuauPolyfill)
6+
local Object = LuauPolyfill.Object
7+
type Array<T> = LuauPolyfill.Array<T>
8+
type Object = LuauPolyfill.Object
9+
10+
local document = require(Packages.JsHelpers.document)
11+
12+
local exports = {}
13+
14+
-- ROBLOX FIXME: use when ready
15+
-- local getQueriesForElement =
16+
-- require(script.Parent.Parent.Parent["get-queries-for-element"]).getQueriesForElement
17+
local getQueriesForElement = function(el: Instance)
18+
return {}
19+
end
20+
21+
local function render(html: Array<Instance>, ref_: Object?)
22+
local ref = (if ref_ == nil then {} else ref_) :: Object
23+
-- ROBLOX deviation START: use ROBLOX instances
24+
-- local container = if ref.container == nil then document:createElement("div") else ref.container
25+
local container
26+
if ref.container == nil then
27+
local div = Instance.new("Frame")
28+
container = div
29+
container.Parent = document
30+
else
31+
container = ref.container
32+
end
33+
container:ClearAllChildren()
34+
for i = 1, #html do
35+
html[i].Parent = container
36+
end
37+
-- ROBLOX deviation END
38+
local containerQueries = getQueriesForElement(container)
39+
local function rerender(newHtml: Array<Instance>)
40+
return render(newHtml, { container = container })
41+
end
42+
return Object.assign({}, { container = container, rerender = rerender }, containerQueries)
43+
end
44+
45+
local function renderIntoDocument(html: Array<Instance>)
46+
-- ROBLOX START deviation: no document.body
47+
return render(html, { container = document })
48+
-- ROBLOX deviation END
49+
end
50+
51+
local function cleanup()
52+
-- ROBLOX deviation START:
53+
-- document.body.innerHTML = ""
54+
document:ClearAllChildren()
55+
-- ROBLOX deviation END
56+
end
57+
58+
afterEach(cleanup)
59+
60+
exports.render = render
61+
exports.renderIntoDocument = renderIntoDocument
62+
exports.cleanup = cleanup
63+
64+
return exports
65+
end

0 commit comments

Comments
 (0)