From 172f4af046d5fd23839d55e89eeca04fb37facd3 Mon Sep 17 00:00:00 2001 From: lme-axelor <102581501+lme-axelor@users.noreply.github.com> Date: Fri, 23 Feb 2024 10:09:01 +0100 Subject: [PATCH] test: improve test cases and story --- .../organisms/FloatingButton.test.js | 59 +++++++++--- .../FloatingButton/FloatingButton.tsx | 4 +- .../organisms/FloatingButton/Indicator.tsx | 2 +- .../organisms/FloatingButton.stories.tsx | 90 ++++++++++++------- 4 files changed, 106 insertions(+), 49 deletions(-) diff --git a/packages/ui/__tests__/components/organisms/FloatingButton.test.js b/packages/ui/__tests__/components/organisms/FloatingButton.test.js index 80e51fab0d..08fec3526b 100644 --- a/packages/ui/__tests__/components/organisms/FloatingButton.test.js +++ b/packages/ui/__tests__/components/organisms/FloatingButton.test.js @@ -20,21 +20,22 @@ import React from 'react'; import {View} from 'react-native'; import {shallow} from 'enzyme'; import {CircleButton, FloatingButton, Text} from '@axelor/aos-mobile-ui'; -import {getGlobalStyles} from '../../tools'; +import {getGlobalStyles, getDefaultThemeColors} from '../../tools'; describe('FloatingButton Component', () => { + const Colors = getDefaultThemeColors(); const props = { actions: [ { key: 1, title: 'Title 1', iconName: 'car', + color: Colors.errorColor, disabled: false, onPress: jest.fn(), }, { key: 2, - title: 'Title 2', iconName: 'motorcycle', disabled: true, onPress: jest.fn(), @@ -73,6 +74,7 @@ describe('FloatingButton Component', () => { it('should give the right iconName and size props to the main button', () => { const iconName = 'check'; + const closeIconName = 'x-lg'; const size = 50; const wrapper = shallow( , @@ -80,6 +82,11 @@ describe('FloatingButton Component', () => { expect(wrapper.find(CircleButton).prop('iconName')).toBe(iconName); expect(wrapper.find(CircleButton).prop('size')).toBe(size); + + wrapper.find(CircleButton).simulate('press'); + + expect(wrapper.find(CircleButton).prop('iconName')).toBe(closeIconName); + expect(wrapper.find(CircleButton).prop('size')).toBe(size); }); it('should render the rights FloatingActionButton when click on the main button', () => { @@ -97,24 +104,48 @@ describe('FloatingButton Component', () => { .at(i) .dive() .find(CircleButton) - .prop('iconName'), - ).toBe(props.actions[i].iconName); + .props(), + ).toMatchObject({ + iconName: props.actions[i].iconName, + disabled: props.actions[i].disabled, + color: props.actions[i].color, + square: true, + }); + + if (props.actions[i].title != null) { + expect( + wrapper + .find('FloatingActionButton') + .at(i) + .dive() + .find(Text) + .prop('children'), + ).toBe(props.actions[i].title); + } else { + expect( + wrapper.find('FloatingActionButton').at(i).dive().find(Text).length, + ).toBe(0); + } + } + }); + + it('should render circle style when defined', () => { + const wrapper = shallow( + , + ); + + expect(wrapper.find(CircleButton).prop('square')).toBe(false); + wrapper.find(CircleButton).simulate('press'); + + for (let i = 0; i < props.actions.length; i++) { expect( wrapper .find('FloatingActionButton') .at(i) .dive() .find(CircleButton) - .prop('disabled'), - ).toBe(props.actions[i].disabled); - expect( - wrapper - .find('FloatingActionButton') - .at(i) - .dive() - .find(Text) - .prop('children'), - ).toBe(props.actions[i].title); + .prop('square'), + ).toBe(false); } }); diff --git a/packages/ui/src/components/organisms/FloatingButton/FloatingButton.tsx b/packages/ui/src/components/organisms/FloatingButton/FloatingButton.tsx index e9e02414e4..18498230a2 100644 --- a/packages/ui/src/components/organisms/FloatingButton/FloatingButton.tsx +++ b/packages/ui/src/components/organisms/FloatingButton/FloatingButton.tsx @@ -115,7 +115,7 @@ const FloatingButton = ({ return []; } - return _actions.filter(action => action.hideIf !== true).reverse(); + return _actions.filter(action => action.hideIf !== true); }, [_actions]); const showIndicator = useMemo( @@ -142,7 +142,7 @@ const FloatingButton = ({ onPress={() => setExpanded(current => !current)} /> ) : null} - {!expanded && ( + {!expanded && useCircleStyle && ( { return ( - key} {...args} /> + key} + {...args} + /> ); }, { @@ -59,14 +65,34 @@ storiesOf('ui/organisms/FloatingButton', module).add( defaultValue: 'pen-fill', control: {type: 'text'}, }, + closeIconName: { + type: 'string', + defaultValue: 'x-lg', + control: {type: 'text'}, + }, size: { - control: { - type: 'range', - min: 15, - max: 75, - step: 5, - }, - defaultValue: 50, + control: {type: 'range', min: 30, max: 100, step: 5}, + defaultValue: 60, + }, + useCircleStyle: { + type: 'boolean', + defaultValue: false, + }, + showTitles: { + type: 'boolean', + defaultValue: true, + }, + showIndicatorOnEdit: { + type: 'boolean', + defaultValue: false, + }, + disableDelete: { + type: 'boolean', + defaultValue: false, + }, + hideAdd: { + type: 'boolean', + defaultValue: false, }, }, },