|
| 1 | +-- ROBLOX upstream: https://github.com/testing-library/react-testing-library/blob/v12.1.5/src/__tests__/events.js |
| 2 | +return function() |
| 3 | + local Packages = script.Parent.Parent.Parent |
| 4 | + |
| 5 | + local JestGlobals = require(Packages.Dev.JestGlobals) |
| 6 | + local jestExpect = JestGlobals.expect |
| 7 | + local jest = JestGlobals.jest |
| 8 | + |
| 9 | + local LuauPolyfill = require(Packages.LuauPolyfill) |
| 10 | + local Array = LuauPolyfill.Array |
| 11 | + |
| 12 | + local React = require(Packages.React) |
| 13 | + |
| 14 | + local ParentModule = require(script.Parent.Parent)(afterEach) |
| 15 | + local render = ParentModule.render |
| 16 | + local fireEvent = ParentModule.fireEvent |
| 17 | + |
| 18 | + -- ROBLOX deviation START: subset with currently handled events |
| 19 | + local eventTypes = { |
| 20 | + { |
| 21 | + type = "Mouse", |
| 22 | + events = { |
| 23 | + { fireEventName = "click", instanceEventName = "Activated" }, |
| 24 | + }, |
| 25 | + elementType = "TextButton", |
| 26 | + }, |
| 27 | + { |
| 28 | + type = "Keyboard", |
| 29 | + events = { |
| 30 | + { fireEventName = "keyDown", instanceEventName = "InputBegan" }, |
| 31 | + { fireEventName = "keyUp", instanceEventName = "InputEnded" }, |
| 32 | + }, |
| 33 | + elementType = "TextBox", |
| 34 | + init = { key = Enum.KeyCode.Return }, |
| 35 | + }, |
| 36 | + } |
| 37 | + -- ROBLOX deviation END |
| 38 | + |
| 39 | + Array.forEach(eventTypes, function(ref) |
| 40 | + local type_, events, elementType, init = ref.type, ref.events, ref.elementType, ref.init |
| 41 | + describe(("%s Events"):format(type_), function() |
| 42 | + Array.forEach(events, function(event: { fireEventName: string, instanceEventName: string }) |
| 43 | + local propName = ("on%s%s"):format(event.fireEventName:sub(1, 1):upper(), event.fireEventName:sub(2)) |
| 44 | + it(("triggers %s"):format(propName), function() |
| 45 | + local ref = React.createRef() |
| 46 | + local spy = jest.fn() |
| 47 | + render( |
| 48 | + React.createElement(elementType, { [React.Event[event.instanceEventName]] = spy, ref = ref }) |
| 49 | + ) |
| 50 | + fireEvent[event.fireEventName](ref.current, init) |
| 51 | + jestExpect(spy).toHaveBeenCalledTimes(1) |
| 52 | + end) |
| 53 | + end) |
| 54 | + end) |
| 55 | + end) |
| 56 | + |
| 57 | + Array.forEach(eventTypes, function(ref) |
| 58 | + local type_, events, elementType, init = ref.type, ref.events, ref.elementType, ref.init |
| 59 | + describe(("Native %s Events"):format(type_), function() |
| 60 | + Array.forEach(events, function(eventName) |
| 61 | + local nativeEventName = eventName.fireEventName:lower() -- The doubleClick synthetic event maps to the dblclick native event |
| 62 | + if nativeEventName == "doubleclick" then |
| 63 | + nativeEventName = "dblclick" |
| 64 | + end |
| 65 | + it(("triggers native %s"):format(tostring(nativeEventName)), function() |
| 66 | + local ref = React.createRef() |
| 67 | + local spy = jest.fn() |
| 68 | + local Element = elementType |
| 69 | + local function NativeEventElement() |
| 70 | + React.useEffect(function() |
| 71 | + local element = ref.current |
| 72 | + local connection = element[eventName.instanceEventName]:Connect(function() |
| 73 | + spy() |
| 74 | + end) |
| 75 | + return function() |
| 76 | + connection:Disconnect() |
| 77 | + end |
| 78 | + end) |
| 79 | + return React.createElement(Element, { ref = ref }) |
| 80 | + end |
| 81 | + render(React.createElement(NativeEventElement, nil)) |
| 82 | + fireEvent[eventName.fireEventName](ref.current, init) |
| 83 | + jestExpect(spy).toHaveBeenCalledTimes(1) |
| 84 | + end) |
| 85 | + end) |
| 86 | + end) |
| 87 | + end) |
| 88 | + |
| 89 | + it("onChange works", function() |
| 90 | + local handleChange = jest.fn() |
| 91 | + -- ROBLOX deviation START: wrap spy to filter Changed Event |
| 92 | + local wrappedHandleChange = function(_instance, property) |
| 93 | + if property == "Text" then |
| 94 | + handleChange() |
| 95 | + end |
| 96 | + end |
| 97 | + -- ROBLOX deviation END |
| 98 | + -- ROBLOX deviation START: replace firstChild with Instance equivalent |
| 99 | + local input = |
| 100 | + render(React.createElement("TextBox", { [React.Event.Changed] = wrappedHandleChange })).container:GetChildren()[1] |
| 101 | + -- ROBLOX deviation END |
| 102 | + fireEvent.change(input, { target = { Text = "a" } }) |
| 103 | + jestExpect(handleChange).toHaveBeenCalledTimes(1) |
| 104 | + end) |
| 105 | + |
| 106 | + it("calling `fireEvent` directly works too", function() |
| 107 | + local handleEvent = jest.fn() |
| 108 | + -- ROBLOX deviation START: replace firstChild with Instance equivalent |
| 109 | + local button = |
| 110 | + render(React.createElement("TextButton", { [React.Event.Activated] = handleEvent })).container:GetChildren()[1] |
| 111 | + -- ROBLOX deviation END |
| 112 | + fireEvent(button, "click") |
| 113 | + end) |
| 114 | + |
| 115 | + -- ROBLOX deviation START: not handled |
| 116 | + -- itSKIP("blur/focus bubbles in react", function() |
| 117 | + -- local handleBlur = jest.fn() |
| 118 | + -- local handleBubbledBlur = jest.fn() |
| 119 | + -- local handleFocus = jest.fn() |
| 120 | + -- local handleBubbledFocus = jest.fn() |
| 121 | + -- local container = render( |
| 122 | + -- React.createElement( |
| 123 | + -- "Frame", |
| 124 | + -- { onBlur = handleBubbledBlur, onFocus = handleBubbledFocus }, |
| 125 | + -- React.createElement("TextButton", { onBlur = handleBlur, onFocus = handleFocus }) |
| 126 | + -- ) |
| 127 | + -- ).container |
| 128 | + -- local button = container:GetChildren()[1]:GetChildren()[1] |
| 129 | + -- fireEvent.focus(button) |
| 130 | + -- jestExpect(handleBlur).toHaveBeenCalledTimes(0) |
| 131 | + -- jestExpect(handleBubbledBlur).toHaveBeenCalledTimes(0) |
| 132 | + -- jestExpect(handleFocus).toHaveBeenCalledTimes(1) |
| 133 | + -- jestExpect(handleBubbledFocus).toHaveBeenCalledTimes(1) |
| 134 | + -- fireEvent.blur(button) |
| 135 | + -- jestExpect(handleBlur).toHaveBeenCalledTimes(1) |
| 136 | + -- jestExpect(handleBubbledBlur).toHaveBeenCalledTimes(1) |
| 137 | + -- jestExpect(handleFocus).toHaveBeenCalledTimes(1) |
| 138 | + -- jestExpect(handleBubbledFocus).toHaveBeenCalledTimes(1) |
| 139 | + -- end) |
| 140 | + -- ROBLOX deviation END |
| 141 | +end |
0 commit comments