Untitled

mail@pastecode.io avatar
unknown
plain_text
21 days ago
1.1 kB
0
Indexable
Never
// MyComponent.test.js
import React from 'react';
import { render } from '@testing-library/react';
import { Provider } from 'react-redux';
import configureStore from 'redux-mock-store'; // Install redux-mock-store as a dev dependency

import MyComponent from './MyComponent';

// Mock Redux store
const mockStore = configureStore([]);

describe('MyComponent', () => {
  let store;

  beforeEach(() => {
    store = mockStore({
      someSlice: {
        myConstValue: 'Original Value', // Initial state for myConstValue
        // Other state slices if needed
      },
    });
  });

  test('should render with mocked useSelector', () => {
    // Mock useSelector to return a specific value
    jest.spyOn(React, 'useSelector').mockImplementation(state => state(store.getState()));

    const { getByText } = render(
      <Provider store={store}>
        <MyComponent />
      </Provider>
    );

    // Assert that the component renders with the mocked value
    expect(getByText('My Const Value: Original Value')).toBeInTheDocument();
  });

  afterEach(() => {
    jest.restoreAllMocks(); // Reset mock after each test
  });
});
Leave a Comment