Background There are numerous examples online for testing react-redux higher order components, eg: react Components using connect(). However, they are all in ES6. I understand the structure and able to run the tests with the help of these docs or examples.
Issue When I try to use the JS code and translate it into Typescritpt, I start getting type issues. Not really sure how this could be done in typescript
The component I'm trying to test is Counter component tha in the AspNetCore React-Redux yoman scaffold
Counter.tsx
import * as React from 'react';
import { Link, RouteComponentProps } from 'react-router-dom';
import { connect } from 'react-redux';
import { ApplicationState } from '../store';
import * as CounterStore from '../store/Counter';
import * as WeatherForecasts from '../store/WeatherForecasts';
type CounterProps =
CounterStore.CounterState
& typeof CounterStore.actionCreators
& RouteComponentProps<{}>;
export class Counter extends React.Component<CounterProps, {}> {
public render() {
return <div>
<h1>Counter</h1>
<p>This is a simple example of a React component.</p>
<p>Current count: <strong>{ this.props.count }</strong></p>
<button onClick={
() => { this.props.increment() }
}>Increment</button>
</div>;
}
}
export default connect(
(state: ApplicationState) => state.counter,
CounterStore.actionCreators
)(Counter) as typeof Counter;
Counter.spec.js
import { Counter} from '../../ClientApp/components/Counter;
import * as Provider from 'react-redux';
import configureMockStore from 'redux-mock-store';
import * as store from '../../ClientApp/store';
import { shallow, mount } from 'enzyme';
declare var describe, beforeEach, it, expect, state, stub;
describe('Counter', () => {
let wrapper;
beforeEach(() => {
wrapper = shallow(<Provider store={store}>
<Counter />
</Provider>
)
});
it('render Counter', () => {
expect(wrapper.length).toEqual(1)
});
});
Error Messages
[ts] JSX element type 'Provider' does not have any construct or call signatures.
[ts] 'React' refers to a UMD global, but the current file is a module. Consider adding an import instead
[ts]
Type '{}' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<Login> & Readonly<{ children?: ReactNode; }> & Rea...'.
Type '{}' is not assignable to type 'Readonly<UserProps>'.
Property 'loggedIn' is missing in type '{}'.
Question
How to get rid of these error messaes
you forgot to import React in your spec file:
import * as React from 'react' // -------------------------------> THIS LINE
import { Counter} from '../../ClientApp/components/Counter;
import * as Provider from 'react-redux';
import configureMockStore from 'redux-mock-store';
import * as store from '../../ClientApp/store';
import { shallow, mount } from 'enzyme';
declare var describe, beforeEach, it, expect, state, stub;
describe('Counter', () => {
let wrapper;
beforeEach(() => {
wrapper = shallow(<Provider store={store}>
<Counter />
</Provider>
)
});
it('render Counter', () => {
expect(wrapper.length).toEqual(1)
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With