Skip to content

Commit

Permalink
Merge pull request #39 from netzwerg/38-vitejs-migration
Browse files Browse the repository at this point in the history
Vite.js Migration
  • Loading branch information
netzwerg authored Feb 1, 2022
2 parents ad767b2 + 454fd20 commit f31da1c
Show file tree
Hide file tree
Showing 28 changed files with 3,341 additions and 5,429 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1 +1 @@
REACT_APP_VERSION=$npm_package_version
# Prefix .env-variables with `VITE_`
13 changes: 5 additions & 8 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
node_modules
.pnp
.pnp.js

# testing
/coverage
coverage

# production
/build
build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
*.local
.idea

npm-debug.log*
Expand Down
2 changes: 1 addition & 1 deletion .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx yarpm-yarn lint-staged && npx tsc && npx yarpm-yarn run lint
npx -p yarpm yarpm-yarn lint-staged && npx tsc && npx -p yarpm yarpm-yarn run lint
11 changes: 5 additions & 6 deletions .storybook/main.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
module.exports = {
stories: ['../src/**/*.stories.mdx', '../src/**/*.stories.@(js|jsx|ts|tsx)'],
addons: [
'@storybook/addon-links',
'@storybook/addon-essentials',
'@storybook/preset-create-react-app',
'storybook-addon-mui-mode',
],
addons: ['@storybook/addon-links', '@storybook/addon-essentials', 'storybook-dark-mode'],
framework: '@storybook/react',
core: {
builder: 'storybook-builder-vite',
},
}
10 changes: 6 additions & 4 deletions .storybook/preview.js → .storybook/preview.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useMemo } from 'react'
import React from 'react'
import { MuiThemeProvider } from '@material-ui/core/styles'
import { useDarkMode } from 'storybook-dark-mode'
import CssBaseline from '@material-ui/core/CssBaseline'
import { darkTheme, lightTheme } from '../src/theme'

Expand All @@ -13,9 +14,10 @@ export const parameters = {
},
}

const withMuiTheme = (Story, context) => {
const mode = context.globals?.muiMode
const theme = useMemo(() => (mode === 'light' ? lightTheme : darkTheme), [mode])
const withMuiTheme = (Story, { args }) => {
const darkMode = useDarkMode() || args.theme === 'dark'
const theme = !darkMode ? lightTheme : darkTheme

return (
<MuiThemeProvider theme={theme}>
<CssBaseline />
Expand Down
4 changes: 2 additions & 2 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ This is how I do it – *you do you* 💖
Deployed live version: ✨ https://netzwerg.github.io/react-you-do-you ✨

== Setup & Tooling
** https://reactjs.org[React 17] based on https://facebook.github.io/create-react-app[create-react-app]:
** https://reactjs.org[React 17] based on https://vitejs.dev[Vite]:
*** Compilation, linting, etc.
*** Development mode with auto-reloading
*** Test watcher
*** Optimized production build
*** see <<scripts, Available Scripts>>
** https://www.typescriptlang.org/[TypeScript 4.3] for compile-time safety
** https://www.typescriptlang.org/[TypeScript 4.4] for compile-time safety
** https://prettier.io[Prettier] for formatting, auto-triggered on commit via https://github.com/typicode/husky[Husky]
** https://redux-toolkit.js.org[Redux Toolkit] for state management
** https://material-ui.com/[Material UI 4] component library (incl. CSS-in-TS)
Expand Down
20 changes: 20 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/src/favicon.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="apple-touch-icon" href="logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="manifest.json" />
<title>Vite App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
64 changes: 38 additions & 26 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
{
"name": "react-you-do-you",
"version": "1.0.0",
"version": "2.0.0",
"private": true,
"homepage": "/react-you-do-you",
"scripts": {
"build": "react-scripts build",
"eject": "react-scripts eject",
"build": "tsc && vite build",
"lint": "eslint './src/**/*.{ts,tsx}'",
"lint:fix": "eslint './src/**/*.{ts,tsx}' --fix",
"start": "react-scripts start",
"test": "react-scripts test",
"storybook": "start-storybook -p 6006 -s public",
"build-storybook": "build-storybook -s public",
"prepreview": "yarn build",
"preview": "vite preview",
"start": "vite",
"test": "vitest",
"coverage": "vitest run --coverage",
"storybook": "start-storybook -p 6006",
"build-storybook": "build-storybook",
"prepare": "husky install"
},
"lint-staged": {
Expand Down Expand Up @@ -60,30 +62,40 @@
"@material-ui/core": "4.12.2",
"@material-ui/icons": "4.11.2",
"@reduxjs/toolkit": "^1.6.1",
"@storybook/addon-actions": "^6.3.6",
"@storybook/addon-essentials": "^6.3.6",
"@storybook/addon-links": "^6.3.6",
"@storybook/node-logger": "^6.3.6",
"@storybook/preset-create-react-app": "^3.2.0",
"@storybook/react": "^6.3.6",
"@testing-library/react": "12.0.0",
"@types/jest": "26.0.24",
"react": "17.0.2",
"react-dom": "17.0.2",
"react-redux": "7.2.4"
},
"devDependencies": {
"@babel/core": "^7.16.12",
"@storybook/addon-actions": "^6.4.15",
"@storybook/addon-essentials": "^6.4.15",
"@storybook/addon-links": "^6.4.15",
"@storybook/node-logger": "^6.4.15",
"@storybook/preset-create-react-app": "^4.0.0",
"@storybook/react": "^6.4.15",
"@testing-library/jest-dom": "^5.16.1",
"@testing-library/react": "^12.1.2",
"@testing-library/react-hooks": "^7.0.2",
"@testing-library/user-event": "^13.5.0",
"@types/node": "16.4.3",
"@types/react": "17.0.15",
"@types/react-dom": "17.0.9",
"@types/react": "^17.0.33",
"@types/react-dom": "^17.0.10",
"@types/react-redux": "7.1.18",
"@vitejs/plugin-react": "^1.0.7",
"babel-loader": "^8.2.3",
"eslint": "^8.7.0",
"eslint-config-react-app": "^7.0.0",
"husky": "^7.0.4",
"jsdom": "latest",
"lint-staged": "11.1.1",
"prettier": "2.3.2",
"prettier-plugin-packagejson": "2.2.11",
"react": "17.0.2",
"react-dom": "17.0.2",
"react-redux": "7.2.4",
"react-scripts": "4.0.3",
"storybook-addon-mui-mode": "^0.0.11",
"typescript": "4.3.5"
},
"resolutions": {
"babel-loader": "8.1.0"
"storybook-builder-vite": "^0.1.13",
"storybook-dark-mode": "^1.0.8",
"typescript": "^4.4.4",
"vite": "^2.7.2",
"vite-plugin-checker": "^0.3.4",
"vitest": "^0.2.5"
}
}
Binary file removed public/favicon.ico
Binary file not shown.
40 changes: 0 additions & 40 deletions public/index.html

This file was deleted.

2 changes: 1 addition & 1 deletion src/app/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const App = () => {
<div className="App">
<AppBar>
<Toolbar className={classes.toolbar}>
<Typography>{`React You Do You – v${process.env.REACT_APP_VERSION}`}</Typography>
<Typography>{`React You Do You – v${import.meta.env.VITE_APP_VERSION}`}</Typography>
<ThemeSwitch />
</Toolbar>
</AppBar>
Expand Down
8 changes: 3 additions & 5 deletions src/app/containers/App.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React from 'react'
import ReactDOM from 'react-dom'
import { render, screen } from '../../test/test-utils'
import { Provider } from 'react-redux'
import { App } from './App'
import { store } from '../../store'
Expand All @@ -10,7 +9,6 @@ it('renders without crashing', () => {
<App />
</Provider>
)
const div = document.createElement('div')
ReactDOM.render(<Root />, div)
ReactDOM.unmountComponentAtNode(div)
render(<Root />)
expect(screen.getByText(/React You Do You/)).toBeInTheDocument()
})
4 changes: 3 additions & 1 deletion src/chat/chatSlice.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { vi } from 'vitest'
import { Response } from 'node-fetch'
import { createStore } from '../store'
import {
addChatError,
Expand Down Expand Up @@ -49,7 +51,7 @@ describe('chatSlice', () => {
const testUrlError = 'test-url-error'
const message = 'Hey there'

globalAny.fetch = jest.fn().mockImplementation((url) => {
globalAny.fetch = vi.fn().mockImplementation((url) => {
switch (url) {
case `${testUrlOk}`:
return Promise.resolve(new Response(message))
Expand Down
8 changes: 3 additions & 5 deletions src/chat/components/ChatErrors.test.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import React from 'react'
import ReactDOM from 'react-dom'
import { render, screen } from '../../test/test-utils'
import { ChatErrors } from './ChatErrors'
import { noOp } from '../../utils'

it('renders without crashing', () => {
const div = document.createElement('div')
ReactDOM.render(<ChatErrors errors={[]} onDismissErrors={noOp} />, div)
ReactDOM.unmountComponentAtNode(div)
render(<ChatErrors errors={[{ error: 'Error' }]} onDismissErrors={noOp} />)
expect(screen.getByText(/error\(s\)/)).toBeInTheDocument()
})
8 changes: 3 additions & 5 deletions src/chat/components/ChatHistory.test.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import React from 'react'
import ReactDOM from 'react-dom'
import { render, screen } from '../../test/test-utils'
import { ChatHistory } from './ChatHistory'
import { noOp } from '../../utils'

it('renders without crashing', () => {
const div = document.createElement('div')
ReactDOM.render(<ChatHistory messages={[]} onDeleteMessage={noOp} />, div)
ReactDOM.unmountComponentAtNode(div)
render(<ChatHistory messages={[{ text: 'Test Message', timestamp: 0 }]} onDeleteMessage={noOp} />)
expect(screen.getByText('Test Message')).toBeInTheDocument()
})
31 changes: 25 additions & 6 deletions src/chat/components/ChatInput.test.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
import React from 'react'
import ReactDOM from 'react-dom'
import { vi } from 'vitest'
import { render, screen, userEvent, waitFor } from '../../test/test-utils'
import { ChatInput } from './ChatInput'
import { noOp } from '../../utils'

it('renders without crashing', () => {
const div = document.createElement('div')
ReactDOM.render(<ChatInput onAddMessage={noOp} onFetchAsyncMessage={noOp} onDemoError={noOp} />, div)
ReactDOM.unmountComponentAtNode(div)
it('renders without crashing', async () => {
const onAddMessageMock = vi.fn()

render(<ChatInput onAddMessage={onAddMessageMock} onFetchAsyncMessage={noOp} onDemoError={noOp} />)

//screen.logTestingPlaygroundURL()

const input = screen.getByRole('textbox')

expect(input).toBeInTheDocument()

userEvent.type(input, 'My custom Message')

expect(input).toHaveValue('My custom Message')

userEvent.type(input, '{Enter}')

await waitFor(() => {
expect(input).toHaveValue('')
})

expect(onAddMessageMock).toHaveBeenCalled()
expect(onAddMessageMock).toHaveBeenCalledWith('My custom Message')
})
15 changes: 15 additions & 0 deletions src/favicon.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
1 change: 0 additions & 1 deletion src/react-app-env.d.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/stories/Introduction.stories.mdx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Meta } from '@storybook/addon-docs/blocks'
import { Meta } from '@storybook/addon-docs'
import Code from './assets/code-brackets.svg'
import Colors from './assets/colors.svg'
import Comments from './assets/comments.svg'
Expand Down
1 change: 1 addition & 0 deletions src/test/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import '@testing-library/jest-dom'
14 changes: 14 additions & 0 deletions src/test/test-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/* eslint-disable import/export */
import { render } from '@testing-library/react'

const customRender = (ui: React.ReactElement, options = {}) =>
render(ui, {
// wrap provider(s) here if needed
wrapper: ({ children }) => children,
...options,
})

export * from '@testing-library/react'
export { default as userEvent } from '@testing-library/user-event'
// override render export
export { customRender as render }
Loading

0 comments on commit f31da1c

Please sign in to comment.