Skip to content

Commit

Permalink
Initial bun testing implementation (blocked by oven-sh/bun#198)
Browse files Browse the repository at this point in the history
  • Loading branch information
EvHaus committed May 20, 2023
1 parent 0df92fc commit 0801cad
Show file tree
Hide file tree
Showing 59 changed files with 1,969 additions and 14 deletions.
Binary file added benchmarks/bun/bun.lockb
Binary file not shown.
6 changes: 6 additions & 0 deletions benchmarks/bun/helpers/css.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import hook from 'css-modules-require-hook';

// Support for CSS modules
hook({
generateScopedName: '[name]__[local]___[hash:base64:5]',
});
19 changes: 19 additions & 0 deletions benchmarks/bun/helpers/happydom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Window } from 'happy-dom';

// Setup happy-dom
const window = new Window();
global.window = window;

// Register global window extensions
[
'document',
'Element',
'getComputedStyle',
'HTMLCanvasElement',
'HTMLElement',
'HTMLInputElement',
'navigator',
'SVGElement'
].forEach((key) => {
global[key] = global.window[key];
});
7 changes: 7 additions & 0 deletions benchmarks/bun/helpers/jsdom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import jsdom from "jsdom";

const { JSDOM } = jsdom;
const dom = new JSDOM('<!doctype html><html><body></body></html>');

global.window = dom.window;
global.document = dom.document;
2 changes: 2 additions & 0 deletions benchmarks/bun/helpers/preload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import './css';
import './happydom';
24 changes: 24 additions & 0 deletions benchmarks/bun/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "bun",
"version": "0.2.0",
"description": "",
"scripts": {
"test": "TZ=UTC bun test --preload './helpers/preload.ts'"
},
"author": "",
"license": "MIT",
"dependencies": {
"@blueprintjs/icons": "4.6.3",
"@floating-ui/react-dom-interactions": "0.10.1",
"clsx": "1.2.1",
"date-fns": "2.29.3",
"framer-motion": "7.5.3",
"react": "18.2.0",
"react-dom": "18.2.0"
},
"devDependencies": {
"css-modules-require-hook": "4.2.3",
"happy-dom": "7.4.0",
"jsdom": "22.0.0"
}
}
1 change: 1 addition & 0 deletions benchmarks/bun/tests/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
replica*
60 changes: 60 additions & 0 deletions benchmarks/bun/tests/original/Alert/Alert.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
.main {
border: 1px solid transparent;
border-radius: 8px;
display: flex;
font-size: 14px;
padding: 16px;
}

.error {
background: var(--R50);
border-color: var(--R400);
color: var(--R400);
}

/* stylelint-disable-next-line selector-max-type, selector-max-combinators */
.error a, .error strong { color: inherit }

.info {
background: var(--N50);
border-color: var(--N400);
color: var(--N400);
}

/* stylelint-disable-next-line selector-max-type, selector-max-combinators */
.info a, .info strong { color: inherit }

.success {
background: var(--G50);
border-color: var(--G400);
color: var(--G400);
}

/* stylelint-disable-next-line selector-max-type, selector-max-combinators */
.success a, .success strong { color: inherit }

.warning {
background: var(--Y50);
border-color: var(--Y400);
color: var(--Y500);
}

/* stylelint-disable-next-line selector-max-type, selector-max-combinators */
.warning a, .warning strong { color: inherit }

.icon {
margin-right: 16px;
}

.title {
color: inherit;
font-weight: bold;
margin: 0 0 8px;
}

.text {
display: flex;
flex-direction: column;
}

.children { color: var(--COLOR-GRAY-800) }
27 changes: 27 additions & 0 deletions benchmarks/bun/tests/original/Alert/Alert.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import {describe, expect, it} from "bun:test";
import Alert from '.';
import React from 'react';
import {render} from '@testing-library/react';

describe('<Alert />', () => {
it('should render the given message', () => {
const {getByText} = render(<Alert>Hello World</Alert>);
expect(getByText('Hello World')).toBeDefined();
});

it('should render with only a title', () => {
const {getByText} = render(<Alert title='Hello World' />);
expect(getByText('Hello World')).toBeDefined();
});

it('should support all the different icons', () => {
const {getByLabelText, rerender} = render(<Alert intent='warning' />);
expect(getByLabelText('warning-sign')).toBeDefined();

rerender(<Alert intent='info' />)
expect(getByLabelText('info-sign')).toBeDefined();

rerender(<Alert intent='success' />)
expect(getByLabelText('tick-circle')).toBeDefined();
});
});
48 changes: 48 additions & 0 deletions benchmarks/bun/tests/original/Alert/Alert.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import Icon, {IconNames} from '../Icon';
import clsx from 'clsx';
import React from 'react';
import styles from './Alert.module.css';

type IntentType = 'error' | 'info' | 'success' | 'warning';

type PropsType = {
className?: string | null,
children?: React.ReactNode,
intent?: IntentType,
title?: string | null,
}

const getIcon = (intent: IntentType) => {
switch (intent) {
case 'info': return IconNames.InfoSign;
case 'success': return IconNames.TickCircle;
case 'warning': return IconNames.WarningSign;
case 'error': return IconNames.Error;
}
return IconNames.Error;
};

const Alert = ({
className,
children,
intent = 'error',
title,
}: PropsType) => {
const classes = clsx(styles.main, styles[intent], className);

return (
<div className={classes} role='alert'>
<Icon
className={styles.icon}
name={getIcon(intent)} />
<div className={styles.text}>
{title ? <h4 className={styles.title}>{title}</h4> : null}
{children ? (
<div className={styles.children}>{children}</div>
) : null}
</div>
</div>
);
};

export default Alert;
1 change: 1 addition & 0 deletions benchmarks/bun/tests/original/Alert/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {default} from './Alert';
9 changes: 9 additions & 0 deletions benchmarks/bun/tests/original/Avatar/Avatar.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.main {
align-items: center;
border-radius: 100%;
display: inline-flex;
font-size: 15px;
height: 40px;
justify-content: center;
width: 40px;
}
11 changes: 11 additions & 0 deletions benchmarks/bun/tests/original/Avatar/Avatar.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {describe, expect, it} from "bun:test";
import Avatar from '.';
import React from 'react';
import {render} from '@testing-library/react';

describe('<Avatar />', () => {
it('should render the avatar with the initials of the title', () => {
const {getByText} = render(<Avatar title='Hello World' />);
expect(getByText('HW')).toBeDefined();
});
});
60 changes: 60 additions & 0 deletions benchmarks/bun/tests/original/Avatar/Avatar.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import clsx from 'clsx';
import React from 'react';
import styles from './Avatar.module.css';

type PropsType = {
className?: string | null,
colorKey?: SupportedColorsType | null,
title?: string | null,
}

type SupportedColorsType = typeof SUPPORTED_COLORS[number];

const SUPPORTED_COLORS = [
'N', 'B', 'G', 'Y', 'R', 'V', 'T', 'P', 'O',
] as const;

const stringToColorIndex = (str: string) => {
// This gives us a number between 0 and 41
const chr = (str.length ? str.charCodeAt(0) : 0) - 49;

// This gives us an index in our supported color range
return Math.floor(chr / (41 / SUPPORTED_COLORS.length));
};

const getColor = (initials: string, colorKey?: string | null) => {
const colorIndex = stringToColorIndex(initials);
const key = colorKey || SUPPORTED_COLORS[colorIndex];
const background = `var(--${key}100)`;
const color = `var(--${key}600)`;
return {background, color};
};

const Avatar = ({
className,
colorKey,
title,
}: PropsType) => {
const classes = clsx(styles.main, className);
const initials = title?.length ?
title
.trim()
.split(' ')
.map((word) => word[0].toUpperCase())
.slice(0, 2)
.join('') :
null;
const {background, color} = getColor(initials || '', colorKey);
const style = {background, color};

return (
<div
className={classes}
style={style}
title={title || ''}>
{initials}
</div>
);
};

export default Avatar;
1 change: 1 addition & 0 deletions benchmarks/bun/tests/original/Avatar/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {default} from './Avatar';
53 changes: 53 additions & 0 deletions benchmarks/bun/tests/original/Badge/Badge.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
.main {
align-items: center;
border-radius: 4px;
display: inline-flex;
font-size: 11px;
height: 16px;
padding: 0 6px;
}

.blue {
background: var(--B200);
color: var(--B600);
}

.green {
background: var(--G300);
color: var(--G600);
}

.neutral {
background: var(--N300);
color: var(--N800);
}

.orange {
background: var(--O200);
color: var(--O600);
}

.purple {
background: var(--P200);
color: var(--P600);
}

.red {
background: var(--R200);
color: var(--R600);
}

.turquoise {
background: var(--T200);
color: var(--T600);
}

.violet {
background: var(--V200);
color: var(--V600);
}

.yellow {
background: var(--Y200);
color: var(--Y600);
}
11 changes: 11 additions & 0 deletions benchmarks/bun/tests/original/Badge/Badge.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import {describe, expect, it} from "bun:test";
import Badge from '.';
import React from 'react';
import {render} from '@testing-library/react';

describe('<Badge />', () => {
it('should render the given badge', () => {
const {getByText} = render(<Badge>Hello World</Badge>);
expect(getByText('Hello World')).toBeDefined();
});
});
23 changes: 23 additions & 0 deletions benchmarks/bun/tests/original/Badge/Badge.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import clsx from 'clsx';
import React from 'react';
import styles from './Badge.module.css';

type PropsType = {
className?: string | null,
children: string,
color?: 'blue' | 'green' | 'neutral' | 'orange' | 'purple' | 'red' | 'turquoise' | 'violet' | 'yellow',
}

const Badge = ({
className,
children,
color = 'neutral',
}: PropsType) => (
<strong
className={clsx(styles.main, styles[color], className)}
title={children}>
{children}
</strong>
);

export default Badge;
1 change: 1 addition & 0 deletions benchmarks/bun/tests/original/Badge/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {default} from './Badge';
Loading

0 comments on commit 0801cad

Please sign in to comment.