Skip to content

Commit

Permalink
feat: add isolated components cypress tests, change test provider for…
Browse files Browse the repository at this point in the history
… cypress components tests
  • Loading branch information
TomatoVan committed Apr 2, 2024
1 parent 3c8fbfa commit f115f56
Show file tree
Hide file tree
Showing 10 changed files with 131 additions and 13 deletions.
7 changes: 7 additions & 0 deletions cypress.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,11 @@ export default defineConfig({
},
baseUrl: 'http://localhost:3000/',
},

component: {
devServer: {
framework: 'react',
bundler: 'webpack',
},
},
});
28 changes: 28 additions & 0 deletions cypress/component/EditableProfileCard.cy.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { EditableProfileCard } from '@/features/editableProfileCard';
import { TestProvider } from '@/shared/lib/tests/componentRender/componentRender';

// isolated component tests

const USER_ID = '1';

describe('EditableProfileCard.cy.tsx', () => {
it('playground', () => {
cy.intercept('GET', '**/profile/*', { fixture: 'profile.json' });
cy.mount(
<TestProvider
options={{
initialState: {
user: {
authData: {
id: USER_ID,
},
},
},
}}
>
<EditableProfileCard id={USER_ID} />
</TestProvider>,
);
// test cases desc
});
});
11 changes: 11 additions & 0 deletions cypress/fixtures/profile.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"id": "1",
"first": "test",
"lastname": "user",
"age": 465,
"currency": "EUR",
"country": "Ukraine",
"city": "Moscow",
"username": "testuser",
"avatar": "https://xakep.ru/wp-content/uploads/2018/05/171485/KuroiSH-hacker.jpg"
}
12 changes: 12 additions & 0 deletions cypress/support/component-index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<title>Components App</title>
</head>
<body>
<div data-cy-root></div>
</body>
</html>
39 changes: 39 additions & 0 deletions cypress/support/component.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// ***********************************************************
// This example support/component.ts is processed and
// loaded automatically before your test files.
//
// This is a great place to put global configuration and
// behavior that modifies Cypress.
//
// You can change the location of this file or turn off
// automatically serving support files with the
// 'supportFile' configuration option.
//
// You can read more here:
// https://on.cypress.io/configuration
// ***********************************************************

// Import commands.js using ES2015 syntax:
import './commands';

// Alternatively you can use CommonJS syntax:
// require('./commands')

import { mount } from 'cypress/react18';

// Augment the Cypress namespace to include type definitions for
// your custom command.
// Alternatively, can be defined in cypress/support/component.d.ts
// with a <reference path="./component" /> at the top of your spec.
declare global {
namespace Cypress {
interface Chainable {
mount: typeof mount
}
}
}

Cypress.Commands.add('mount', mount);

// Example use:
// cy.mount(<MyComponent />)
2 changes: 1 addition & 1 deletion cypress/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"isolatedModules": false
},
"extends": "../tsconfig.json",
"include": ["**/*.ts"]
"include": ["**/*.ts", "**/*.tsx"]
}
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
"eslint-plugin-i18next": "^5.1.2",
"eslint-plugin-import": "^2.25.4",
"eslint-plugin-jsx-a11y": "^6.5.1",
"eslint-plugin-paths-fixes": "^0.0.9",
"eslint-plugin-paths-fixes": "^0.0.10",
"eslint-plugin-react": "^7.29.2",
"eslint-plugin-react-hooks": "^4.3.0",
"eslint-plugin-storybook": "^0.6.7",
Expand Down
29 changes: 25 additions & 4 deletions src/shared/lib/tests/componentRender/componentRender.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,48 @@ import { MemoryRouter } from 'react-router-dom';
import { ReducersMapObject } from '@reduxjs/toolkit';
import i18nForTests from '@/shared/config/i18n/i18nForTests';
import { StateSchema, StoreProvider } from '@/app/providers/StoreProvider';
// eslint-disable-next-line paths-fixes/layer-imports
import { ThemeProvider } from '@/app/providers/ThemeProvider';
import { Theme } from '@/shared/const/theme';
// eslint-disable-next-line paths-fixes/layer-imports
import '@/app/styles/index.scss';

export interface componentRenderOptions {
route?: string;
initialState?: DeepPartial<StateSchema>;
asyncReducers?: DeepPartial<ReducersMapObject<StateSchema>>
theme?: Theme
}

export function componentRender(component: ReactNode, options: componentRenderOptions = {}) {
interface TestProviderProps {
children: ReactNode
options?: componentRenderOptions
}

export function TestProvider(props: TestProviderProps) {
const { children, options = {} } = props;
const {
route = '/',
initialState,
asyncReducers,
theme,
} = options;

return render(
return (
<MemoryRouter initialEntries={[route]}>
<StoreProvider asyncReducers={asyncReducers} initialState={initialState}>
<I18nextProvider i18n={i18nForTests}>
{component}
<ThemeProvider initialTheme={theme}>
<div className={`app ${theme}`}>
{children}
</div>
</ThemeProvider>
</I18nextProvider>
</StoreProvider>
</MemoryRouter>,
</MemoryRouter>
);
}

export function componentRender(component: ReactNode, options: componentRenderOptions = {}) {
return render(<TestProvider options={options}>{component}</TestProvider>);
}
6 changes: 3 additions & 3 deletions webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ export default (env: BuildEnv) => {
buildLocales: path.resolve(__dirname, 'build', 'locales'),
};

const mode = env.mode || 'development';
const PORT = env.port || 3000;
const mode = env?.mode || 'development';
const PORT = env?.port || 3000;

const isDev = mode === 'development';
const apiUrl = env.apiUrl || 'http://localhost:8000';
const apiUrl = env?.apiUrl || 'http://localhost:8000';

const config: webpack.Configuration = buildWebpackConfig({
mode,
Expand Down

0 comments on commit f115f56

Please sign in to comment.