Skip to content
This repository has been archived by the owner on Dec 13, 2023. It is now read-only.

Shallow wrapper #223

Open
wants to merge 67 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 65 commits
Commits
Show all changes
67 commits
Select commit Hold shift + click to select a range
72c6921
Implement shallow wrapper
jeparlefrancais Jul 2, 2019
2b7dde9
Add depth to children wrapper
jeparlefrancais Jul 2, 2019
f57fa89
Support fragments
jeparlefrancais Jul 2, 2019
7ee2939
Filter children prop from shallow.props
jeparlefrancais Jul 9, 2019
45b48da
Add ShallowWrapper:getChildren()
jeparlefrancais Jul 9, 2019
8450335
Split deep equal comparison from assertDeepEqual
jeparlefrancais Jul 19, 2019
a1f914f
Add snapshot serialization
jeparlefrancais Jul 19, 2019
17d8146
Add ref serialization
jeparlefrancais Jul 23, 2019
4183766
Format floats with maximum 7 decimals
jeparlefrancais Jul 25, 2019
830c0b6
Remove Roact.shallow for virtualTree method
jeparlefrancais Jul 25, 2019
39eae67
Rename failed snapshot to new snapshot
jeparlefrancais Jul 25, 2019
16e1ba0
Update snapshot tests
jeparlefrancais Jul 26, 2019
c81d22f
Add snapshot tests
jeparlefrancais Jul 26, 2019
9b09be8
Update snapshot tests
jeparlefrancais Jul 26, 2019
5944bf3
Remove missing lemur events
jeparlefrancais Jul 30, 2019
ae299a5
Pass virtual mounted state in constructor
jeparlefrancais Aug 5, 2019
60b18f2
Rename Snapshot to SnapshotMatcher
jeparlefrancais Aug 5, 2019
2773da6
Rename SnapshotData to Snapshot
jeparlefrancais Aug 5, 2019
edb1ca3
Fix variable shadowing
jeparlefrancais Aug 5, 2019
d73ebf1
Snapshot support table as prop values
jeparlefrancais Aug 5, 2019
f4844ac
Move files into a single folder
jeparlefrancais Aug 7, 2019
a541ec4
Revert VirtualTree
jeparlefrancais Aug 7, 2019
8733795
Rename Snapshot constructor
jeparlefrancais Aug 7, 2019
1b8e6e8
Add API to get snapshot string
jeparlefrancais Aug 7, 2019
0a187d9
Remove children caching on ShallowWrapper
jeparlefrancais Aug 7, 2019
a843b91
Add shallow to global api
jeparlefrancais Aug 7, 2019
caca4ed
Fix format issue
jeparlefrancais Aug 8, 2019
f1da1a2
Rename snapshot to string functions
jeparlefrancais Aug 8, 2019
8dcd096
Fix folder casing
jeparlefrancais Aug 8, 2019
6bd217e
Refactor constraints to be self contained
jeparlefrancais Aug 8, 2019
90a0e40
Add VirtualTree
jeparlefrancais Aug 9, 2019
10c3041
Fix single quotes
jeparlefrancais Aug 9, 2019
846ffd9
Make the ShallowWrapper API strict
jeparlefrancais Aug 9, 2019
764c00a
Update API docs
jeparlefrancais Aug 9, 2019
a36239c
Add snapshots contributing guidelines
jeparlefrancais Aug 9, 2019
7ef84d3
Add plugin to keep snapshots from run to edit mode
jeparlefrancais Aug 9, 2019
dd5bc30
Remove Roact.shallow from docs
jeparlefrancais Aug 12, 2019
3230ad4
Add run-in-roblox docs to contributing guide
jeparlefrancais Aug 12, 2019
8986ce0
Move constraints next to find
jeparlefrancais Aug 13, 2019
9324979
Add shallow rendering page to docs (first pass)
jeparlefrancais Aug 13, 2019
5efa96c
Restructure Shallow Rendering docs
jeparlefrancais Aug 13, 2019
342bbec
Improve snapshot workflow section
jeparlefrancais Aug 13, 2019
750f669
Update snapshot review section
jeparlefrancais Aug 13, 2019
12539ec
Rename ShallowWrapper method toSnapshotString
jeparlefrancais Aug 13, 2019
f1dac37
Remove kind constraint
jeparlefrancais Aug 14, 2019
cfd055a
Improve coverage of validateShallowOptions
jeparlefrancais Aug 14, 2019
4bb8234
Remove unused require
jeparlefrancais Aug 14, 2019
6d677c3
Edit shallow rendering doc to use less second person and tighten up w…
MisterUncloaked Aug 14, 2019
89b3a8e
Merge pull request #2 from MisterUncloaked/shallow-wrapper
jeparlefrancais Aug 14, 2019
7073e0d
Remove ShallowWrapper:getChildren
jeparlefrancais Aug 14, 2019
2764cbc
Remove ShallowWrapper:getChildrenCount()
jeparlefrancais Aug 14, 2019
3f44eab
Rename ShallowWrapper getInstance to getHostObject
jeparlefrancais Aug 15, 2019
0699e75
Add Rect datatype to serializer
jeparlefrancais Aug 16, 2019
d5bcf88
Remove hostKey from top component in snapshots
jeparlefrancais Aug 16, 2019
90010ec
Add depth and find/findUnique examples
jeparlefrancais Aug 16, 2019
5b99367
Throw error when generating snapshots
jeparlefrancais Aug 17, 2019
27db6d9
Update shallow rendering docs
jeparlefrancais Aug 17, 2019
1b1553d
Add snapshots files
jeparlefrancais Aug 19, 2019
ab59379
Merge branch 'master' into shallow-wrapper
jeparlefrancais Aug 19, 2019
13edb92
Remove generated trailing whitespaces in snapshots
jeparlefrancais Aug 20, 2019
ead931b
Move trailing whitespace tests
jeparlefrancais Aug 20, 2019
6dc2133
Remove ElementKind from ShallowWrapper
jeparlefrancais Aug 21, 2019
aec002f
Fix typos and unused parameter
jeparlefrancais Aug 21, 2019
4a83085
Refactor Virtual.mount
jeparlefrancais Aug 21, 2019
01529ba
Simplify IndentedOutput
jeparlefrancais Aug 21, 2019
9f366de
Change getShallowWrapper parameters
jeparlefrancais Aug 22, 2019
780fc4f
Fix shallow rendering docs typo
jeparlefrancais Aug 22, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,24 @@ When submitting a new feature, add tests for all functionality.

We use [LuaCov](https://keplerproject.github.io/luacov) for keeping track of code coverage. We'd like it to be as close to 100% as possible, but it's not always possible. Adding tests just for the purpose of getting coverage isn't useful; we should strive to make only useful tests!

### Snapshots
As part of the test suite, there are tests that generates snapshot files (located under `./RoactSnapshots`). To sync back the generated StringValues produce by these on the file system, we suggest using a tool like [`run-in-roblox`](https://github.com/LPGhatguy/run-in-roblox) to make the workflow as simple as possible. In the case where you only need to sync a few files, you can use the plugin to automatically copy back the generated string values from the run mode into module scripts when going back to edit mode. Then simply copy-paste into the new snapshots into the file-system.

#### Using run-in-roblox
You can install run-in-roblox using [cargo](https://doc.rust-lang.org/cargo/getting-started/installation.html) with the following command.
```
cargo install run-in-roblox
```
Once installed, run the [syncing script](bin/sync_snapshots.lua) to execute the tests and sync the created ModuleScripts from Roblox Studio to the file system. This script works by doing 3 tasks:
1. Build a test place with rojo
2. Use run-in-roblox to run [run-tests-snapshots.lua](bin/run-tests-snapshots.lua) in the place file
3. Parse the output of run-in-roblox to copy back the new snapshots to the file system

Run this script using lua stand alone interpreter
```
lua bin/run-tests-snapshots.lua
```

## Release Checklist
When releasing a new version of Roact, do these things:

Expand Down
19 changes: 19 additions & 0 deletions RoactSnapshots/component-with-event-props.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
return function(dependencies)
local Roact = dependencies.Roact
local ElementKind = dependencies.ElementKind
local Markers = dependencies.Markers

return {
type = {
kind = ElementKind.Host,
className = "TextButton",
},
props = {
[Roact.Event.Activated] = Markers.AnonymousFunction,
[Roact.Event.MouseButton1Click] = Markers.AnonymousFunction,
[Roact.Change.AbsoluteSize] = Markers.AnonymousFunction,
[Roact.Change.Visible] = Markers.AnonymousFunction,
},
children = {},
}
end
35 changes: 35 additions & 0 deletions RoactSnapshots/function-component-children.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
return function(dependencies)
local Roact = dependencies.Roact
local ElementKind = dependencies.ElementKind
local Markers = dependencies.Markers

return {
type = {
kind = ElementKind.Host,
className = "Frame",
},
props = {},
children = {
{
type = {
kind = ElementKind.Function,
},
hostKey = "LabelA",
props = {
Text = "I am label A",
},
children = {},
},
{
type = {
kind = ElementKind.Function,
},
hostKey = "LabelB",
props = {
Text = "I am label B",
},
children = {},
},
},
}
end
23 changes: 23 additions & 0 deletions RoactSnapshots/host-frame-with-multiple-props.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
return function(dependencies)
local Roact = dependencies.Roact
local ElementKind = dependencies.ElementKind
local Markers = dependencies.Markers

return {
type = {
kind = ElementKind.Host,
className = "Frame",
},
props = {
AnchorPoint = Vector2.new(0, 0.5),
BackgroundColor3 = Color3.new(0.1, 0.2, 0.3),
BackgroundTransparency = 0.205,
ClipsDescendants = false,
Size = UDim2.new(0.5, 0, 0.4, 1),
SizeConstraint = Enum.SizeConstraint.RelativeXY,
Visible = true,
ZIndex = 5,
},
children = {},
}
end
28 changes: 28 additions & 0 deletions RoactSnapshots/stateful-component-children.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
return function(dependencies)
local Roact = dependencies.Roact
local ElementKind = dependencies.ElementKind
local Markers = dependencies.Markers

return {
type = {
kind = ElementKind.Host,
className = "Frame",
},
props = {},
children = {
{
type = {
kind = ElementKind.Stateful,
componentName = "CoolComponent",
},
hostKey = "Child",
props = {
label = {
Text = "foo",
},
},
children = {},
},
},
}
end
41 changes: 41 additions & 0 deletions SnapshotsPlugin/EditModeMain.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Settings = require(script.Parent.Settings)

local function SyncSnapshots(newSnapshots)
local snapshotsFolder = ReplicatedStorage:FindFirstChild(Settings.SnapshotFolderName)

if not snapshotsFolder then
snapshotsFolder = Instance.new("Folder")
snapshotsFolder.Name = Settings.SnapshotFolderName
snapshotsFolder.Parent = ReplicatedStorage
end

for name, value in pairs(newSnapshots) do
local snapshot = Instance.new("ModuleScript")
snapshot.Name = name
snapshot.Source = value
snapshot.Parent = snapshotsFolder
end
end

local function PluginEditMode(plugin)
local isPluginDeactivated = false

plugin.Deactivation:Connect(function()
isPluginDeactivated = true
end)

while not isPluginDeactivated do
local newSnapshots = plugin:GetSetting(Settings.PluginSettingName)

if newSnapshots then
SyncSnapshots(newSnapshots)
plugin:SetSetting(Settings.PluginSettingName, false)
end

wait(Settings.SyncDelay)
end
end

return PluginEditMode
25 changes: 25 additions & 0 deletions SnapshotsPlugin/RunModeMain.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Settings = require(script.Parent.Settings)

local function PluginRunMode(plugin)
plugin.Unloading:Connect(function()
local snapshotsFolder = ReplicatedStorage:FindFirstChild(Settings.SnapshotFolderName)

local newSnapshots = {}

if not snapshotsFolder then
return
end

for _, snapshot in pairs(snapshotsFolder:GetChildren()) do
if snapshot:IsA("StringValue") then
newSnapshots[snapshot.Name] = snapshot.Value
end
end

plugin:SetSetting(Settings.PluginSettingName, newSnapshots)
end)
end

return PluginRunMode
17 changes: 17 additions & 0 deletions SnapshotsPlugin/Settings.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
local function IndexError(_, key)
local message = ("%q (%s) is not a valid member of Settings"):format(
tostring(key),
typeof(key)
)

error(message, 2)
end

return setmetatable({
SnapshotFolderName = "RoactSnapshots",
PluginSettingName = "NewRoactSnapshots",
SyncDelay = 1,
}, {
__index = IndexError,
__newindex = IndexError,
})
12 changes: 12 additions & 0 deletions SnapshotsPlugin/init.server.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
local RunService = game:GetService("RunService")

local EditModeMain = require(script.EditModeMain)
local RunModeMain = require(script.RunModeMain)

if RunService:IsEdit() then
EditModeMain(plugin)
else
if RunService:IsClient() then
RunModeMain(plugin)
end
end
26 changes: 26 additions & 0 deletions bin/run-tests-snapshots.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
-- luacheck: globals __LEMUR__

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Roact = require(ReplicatedStorage.Roact)
local TestEZ = require(ReplicatedStorage.TestEZ)

Roact.setGlobalConfig({
["internalTypeChecks"] = true,
["typeChecks"] = true,
["elementTracing"] = true,
["propValidation"] = true,
})
local results = TestEZ.TestBootstrap:run(ReplicatedStorage.Roact, TestEZ.Reporters.TextReporter)

local RoactSnapshots = ReplicatedStorage:WaitForChild("RoactSnapshots", 1)

if not RoactSnapshots then
return nil
end

for _, snapshot in pairs(RoactSnapshots:GetChildren()) do
if snapshot:IsA("StringValue") then
print(("Snapshot:::<|%s|><|=>%s<=|>"):format(snapshot.Name, snapshot.Value))
end
end
1 change: 1 addition & 0 deletions bin/spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
local LOAD_MODULES = {
{"src", "Roact"},
{"modules/testez/lib", "TestEZ"},
{"RoactSnapshots", "RoactSnapshots"},
}

-- This makes sure we can load Lemur and other libraries that depend on init.lua
Expand Down
64 changes: 64 additions & 0 deletions bin/sync_snapshots.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
local lfs = require("lfs")

local ROJO_PROJECT_FILE = "place.project.json"
local TEST_FILE = "bin/run-tests-snapshots.lua"

local SNAPSHOTS_FOLDER = "RoactSnapshots"
local TEST_PLACE_FILE_NAME = "temp-snapshot-place.rbxlx"

local function writeSnapshotFile(name, content)
lfs.mkdir(SNAPSHOTS_FOLDER)

local fileName = name .. ".lua"
local filePath = ("%s/%s"):format(SNAPSHOTS_FOLDER, fileName)

print("Writing", filePath)

local file = io.open(filePath, "w")
file:write(content)
file:close()
end

local function executeCommand(command)
print(command)
local handle = io.popen(command)
local line = handle:read("*l")
local output = {line}

while line do
line = handle:read("*l")
table.insert(output, line)
end

handle:close()

return table.concat(output, "\n")
end

print("Building test place")
executeCommand(("rojo build %s -o %s"):format(
ROJO_PROJECT_FILE,
TEST_PLACE_FILE_NAME
))

print("Running run-in-roblox")
local output = executeCommand(("run-in-roblox %s -s %s -t 100"):format(
TEST_PLACE_FILE_NAME,
TEST_FILE
))

print("Clean test place")
os.remove(TEST_PLACE_FILE_NAME)

print("Processing output...")

local filteredOutput = output:gsub("\nSnapshot:::<|[%w_%-%.]+|><|=>.-<=|>", "")

print(filteredOutput, "\n")

for snapshotPattern in output:gmatch("Snapshot:::<|[%w_%-%.]+|><|=>.-<=|>") do
local name = snapshotPattern:match("Snapshot:::<|([%w_%-%.]+)|>")
local content = snapshotPattern:match("<|=>(.-)<=|>")

writeSnapshotFile(name, content)
end
Loading