def test_create_element(self): element = create_element('foo', {'bar': 'baz'}) self.assertEqual(element, { 'type': 'foo', 'props': { 'bar': 'baz' }, }) # children key not present in props if not supplied self.assertNotIn('children', element['props']) # when supplied, `children` get added to props element = create_element( 'foo', {'bar': 'baz'}, children=[create_element('some-elm', {'some': 'value'})]) self.assertEqual( element, { 'type': 'foo', 'props': { 'bar': 'baz', 'children': [{ 'type': 'some-elm', 'props': { 'some': 'value' } }] } })
def test_wsx(self): # calling as function result = wsx(['foo', {'bar': 'baz'}]) self.assertEqual(result, create_element('foo', {'bar': 'baz'})) @wsx def my_component(props): return ['foo', props] # asserting the function name gets @wrapped correctly self.assertEqual(my_component.__name__, 'my_component') self.assertEqual(my_component({'bar': 'baz'}), create_element('foo', {'bar': 'baz'}))
def testUriUpdatesChangeImage(self): """ Testing that swapping URI leads to the new images being loaded. """ app = wx.App() with self.subTest("initializing with no URI"): component = wsx( [c.Frame, {'show': True, 'size': (100, 100)}, [c.StaticBitmap, {}]]) frame = render(component, None) bitmap = frame.Children[0] # no images loaded self.assertEqual(bitmap.GetBitmap().RefData, None) with self.subTest('Adding a URI loads the image'): patch(bitmap, create_element(c.StaticBitmap, {'uri': images.red_uri})) original_rgb = self.get_rgb(bitmap) self.assertEqual(original_rgb, images.red_rgb) with self.subTest("updating props with new URI"): patch(bitmap, create_element(c.StaticBitmap, {'uri': images.pink_uri})) updated_rgb = self.get_rgb(bitmap) # colors should now be different because the image was updated self.assertNotEqual(original_rgb, updated_rgb) # and we should have our pink color: self.assertEqual(updated_rgb, images.pink_rgb) with self.subTest("updating props with same URI keeps existing image"): # attaching a mock so we can assert the same URI doesn't cause # a reloading of the image mock = MagicMock() bitmap.SetBitmap = mock patch(bitmap, create_element(c.StaticBitmap, {'uri': images.pink_uri})) mock.assert_not_called()
def _build_app(build_spec, app) -> Tuple[Any, wx.Frame]: """ Note: this method is broken out with app as an argument to facilitate testing. """ # use actual program name instead of script file name in macOS menu app.SetAppDisplayName(build_spec['program_name']) i18n.load(build_spec['language_dir'], build_spec['language'], build_spec['encoding']) imagesPaths = image_repository.loadImages(build_spec['image_dir']) gapp2 = render(create_element(RGooey, merge(build_spec, imagesPaths)), None) # wx.lib.inspection.InspectionTool().Show() # gapp.Show() gapp2.Show() return (app, gapp2)
def test_update_component(self): app = wx.App() frame = render(create_element(Parent, {}), None) # app.MainLoop() # sanity checking that state/props align for initial render initial_parent_state = frame._instance.state['label'] initial_child_props = frame.Children[0]._instance.props['label'] self.assertEqual(initial_parent_state, initial_child_props) # bug #0002 was that child component weren't receiving new props # up update. So now we fire one of the rewx handlers: frame._instance.update_label('foobar') next_parent_state = frame._instance.state['label'] next_child_props = frame.Children[0]._instance.props['label'] # and expect that both are still in sync self.assertEqual(next_parent_state, 'foobar') self.assertEqual(next_parent_state, next_child_props) app.Destroy()
'click_handler': self.update_value, 'label': 'Click me!' }]]] ) class ChildProps(TypedDict): """Optionally typing the props of your component""" label: str click_handler: Callable[[wx.CommandEvent], None] class Child(Component): """ An arbitrary component. Note that this is still pure -- it holds no state of its own and all behaviors, like the click handler, are passed in via props. """ def __init__(self, props: ChildProps): super().__init__(props) def render(self): return wsx([c.Button, { 'on_click': self.props['click_handler'], 'label': self.props['label'] }]) if __name__ == '__main__': app = wx.App() frame = render(create_element(Parent, {}), None) app.MainLoop()
def populated_element(self, type): return create_element(type, valid_props.get(type, {**base_props}))
def empty_element(self, type): return create_element(type, {})
results = list(executor.map(fake_download, args)) wx.CallAfter(self.finish_download) def render(self): return wsx( [Block, {}, [Block, {'orient': wx.HORIZONTAL}, [StaticText, {'label': 'Download #1'}], [Gauge, {"value": self.state["download_1"], "range": 100}]], [Block, {'orient': wx.HORIZONTAL}, [StaticText, {'label': 'Download #2'}], [Gauge, {"value": self.state["download_2"], "range": 100}]], [Button, {"label": "Download" if self.state['status'] == 'READY' else 'In Progress', 'enabled': self.state['status'] == 'READY', "on_click": self.start_download, "flag": wx.CENTER | wx.ALL}], ] ) if __name__ == "__main__": app = wx.App() frame = wx.Frame(None, title="Gauge With Update") body = render(create_element(FooGauge, {}), frame) frame.Show() app.MainLoop()
self.state = {'value': ''} self.input_ref = Ref() def component_did_mount(self): wx_ctrl = self.input_ref.instance wx_ctrl.Bind(wx.EVT_TEXT_ENTER, self.on_enter) def on_input(self, event): self.set_state({'value': event.EventObject.Value}) def on_enter(self, event): wx.MessageDialog(None, "Enter pressed!").ShowModal() def render(self): return wsx( [c.Frame, {'title': 'My Cool Application', 'show': True}, [c.Block, {'orient': wx.HORIZONTAL}, [c.TextCtrl, { 'style': wx.TE_PROCESS_ENTER, 'value': self.state['value'], 'on_change': self.on_input, 'ref': self.input_ref, }]]] ) if __name__ == '__main__': app = wx.App() frame = render(create_element(MyComponent, {}), None) app.MainLoop()
def render(self): return create_element(c.StaticText, {'label': self.props['label']})