Skip to content

Commit 5ed0465

Browse files
committed
add button-test.js
1 parent 4c0947a commit 5ed0465

File tree

2 files changed

+79
-18
lines changed

2 files changed

+79
-18
lines changed

packages/react-ape/renderer/elements/Button.js

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77

88
import {ButtonDefaults} from '../constants';
99

10+
//TODO adjust Opacity when focus, Blur
11+
type PressEvent = {||};
1012
type ButtonProps = {|
1113
title: string,
12-
onPress: (event?: any) => mixed,
14+
onPress: (event?: PressEvent) => mixed,
1315
touchSoundDisabled?: ?boolean,
1416
color?: ?string,
1517
/**
@@ -62,34 +64,34 @@ type ButtonProps = {|
6264
testID?: ?string,
6365
|};
6466

65-
function renderButton(props, apeContext, parentLayout) {
66-
const {spatialGeometry = {}, relativeIndex} = parentLayout;
67+
function renderButton(props: ButtonProps, apeContext, parentLayout) {
68+
const {spatialGeometry = {x: 0, y: 0}, relativeIndex} = parentLayout;
6769
const {ctx} = apeContext;
6870
// If is relative and x and y haven't be processed, don't render
6971
if (!spatialGeometry) return null;
7072
// start drawing the canvas
7173
const {title, color} = props;
7274
const borderRadius = ButtonDefaults.containerStyle.borderRadius;
7375
const backgroundColor = ButtonDefaults.containerStyle.backgroundColor;
74-
let x = spatialGeometry.x || 20;
75-
let y = spatialGeometry.y || 40;
76-
const width = x + y;
77-
const height = ButtonDefaults.containerStyle.height;
76+
let x = spatialGeometry.x || 0;
77+
let y = spatialGeometry.y || 0;
78+
let width = x + y;
79+
let height = ButtonDefaults.containerStyle.height;
80+
// if(parentLayout && parentLayout.style){
81+
// width = parentLayout.style.width
82+
// height = parentLayout.style.height
83+
// }
7884
ctx.beginPath();
79-
//ctx.lineWidth = borderWidth
8085
ctx.fillStyle = backgroundColor;
81-
//ctx.strokeStyle = borderColor
82-
//ctx.rect(x,y,borderRadius,width,height,antiClockWise)
83-
//ctx.rect(x,y,width,height)
8486
ctx.moveTo(x, y);
8587
/**
86-
* Top Right Radius
87-
*/
88+
* Top Right Radius
89+
*/
8890
ctx.lineTo(x + width - borderRadius, y);
8991
ctx.quadraticCurveTo(x + width, y, x + width, y + borderRadius);
9092
/**
91-
* Bottom right Radius
92-
*/
93+
* Bottom right Radius
94+
*/
9395

9496
ctx.lineTo(x + width, y + height - borderRadius);
9597
ctx.quadraticCurveTo(
@@ -100,8 +102,8 @@ function renderButton(props, apeContext, parentLayout) {
100102
);
101103

102104
/**
103-
* Bottom Left Radius
104-
*/
105+
* Bottom Left Radius
106+
*/
105107
ctx.lineTo(x + borderRadius, y + height);
106108
ctx.quadraticCurveTo(x, y + height, x, y + height - borderRadius);
107109
/** Top left Radius */
@@ -119,7 +121,7 @@ function renderButton(props, apeContext, parentLayout) {
119121
ctx.closePath();
120122
}
121123

122-
export default function createButtonInstance(props: Props): mixed {
124+
export default function createButtonInstance(props: ButtonProps): mixed {
123125
return {
124126
type: 'Button',
125127
render: renderButton.bind(this, props),
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import React from 'react';
2+
import renderer from 'react-test-renderer';
3+
import {ButtonDefaults} from '../../constants'
4+
import CreateButtonInstance from '../Button';
5+
6+
describe('Button', () => {
7+
describe('Call the button with Props', () => {
8+
it('Should render properly', () => {
9+
const title = 'Press Me'
10+
const color = '#f8a978'
11+
const x = 40
12+
const y = 20
13+
const width = x + y
14+
const height = ButtonDefaults.containerStyle.height
15+
const props = {title, color};
16+
const apeContext = {
17+
ctx: {
18+
beginPath: jest.fn(),
19+
fillStyle: jest.fn(),
20+
moveTo: jest.fn(),
21+
fillText: jest.fn(),
22+
fill: jest.fn(),
23+
stroke: jest.fn(),
24+
closePath: jest.fn(),
25+
lineTo: jest.fn(),
26+
quadraticCurveTo: jest.fn(),
27+
font: 'Helvetica',
28+
},
29+
};
30+
31+
const Button = CreateButtonInstance(props);
32+
Button.render(apeContext, {spatialGeometry: {x, y}});
33+
const {
34+
beginPath,
35+
fillStyle,
36+
moveTo,
37+
fillText,
38+
fill,
39+
stroke,
40+
closePath,
41+
lineTo,
42+
quadraticCurveTo,
43+
font,
44+
} = apeContext.ctx;
45+
expect(beginPath.mock.calls.length).toBe(1);
46+
expect(beginPath).toBeCalledWith()
47+
expect(closePath).toBeCalledWith()
48+
expect(stroke).toBeCalledWith()
49+
expect(moveTo).toBeCalledWith(x,y)
50+
expect(lineTo.mock.calls.length).toEqual(4)
51+
expect(fill.mock.calls.length).toBe(1)
52+
expect(fillText.mock.calls.length).toBe(1)
53+
expect(fillText).toBeCalledWith(title,x + width/2,y+height/2)
54+
expect(font).toEqual(`${ButtonDefaults.textStyle.fontSize} Helvetica`)
55+
expect(quadraticCurveTo.mock.calls.length).toEqual(4)
56+
expect(fillStyle).toBe(color)
57+
});
58+
});
59+
});

0 commit comments

Comments
 (0)