Skip to content

Instantly share code, notes, and snippets.

View radzserg's full-sized avatar

Sergey Radzishevskii radzserg

View GitHub Profile
import backendApi from "./path/to/backendApi";
import history from "./path/to/configureHistory";
export function* saveProduct(
action: ISaveProductAction
) {
const productResult = backendApi.saveProduct(action.productId, action.productData);
yield put(saveUserDetails(userDetails));
history.push("/products");
return true;
/*
/ProductUpdatePage
- connected.ts
- ProductUpdatePage.ts
- index.ts
- saga.ts
- reducer.ts
*/
// ProductUpdatePage.ts
export default function* configureSagas() {
yield all([
watchLoadProducts(),
watchSaveProduct(),
// all the watchers that we need from all
// parts of our app
]);
}
// and a specfic saga will look like
// now I wanna make a snapshot testing
it("renders home page", () => {
const push = jest.fn()
const mockHistory = { push } as unknown as History;
const page = mount(
<HomePage history={mockHistory}/>
);
expect(page).toMatchSnapshot();
// when you need to test a push
import * as React from "react";
import DIContext from "./DIContext";
import { IDIContainer } from "rsdi";
interface DIResolutionConfig {
[propName: string]: string;
}
type Without<T, K> = {
/HomePage/HomePage.tsx
import { History } from "history";
interface IProps {
label: string;
history: History;
}
class HomePage extends PureComponent<IProps> {
render() {
return (
// DIContext.ts
import React from "react";
import DIContainer, { IDIContainer } from "rsdi";
const DIContext = React.createContext<IDIContainer>(new DIContainer());
export default DIContext;
// App.ts
// build container only once
import DIContainer, { object, get, value, factory, IDIContainer } from "rsdi";
const config = {
"ENV": value("PRODUCTION"), // define raw value
"Storage": object(CookieStorage), // will instantiate an object of class CookieStorage without arguments
"AuthStorage": object(AuthStorage).construct(
get("Storage") // refer to dependency described above
),
"BrowserHistory": factory(configureHistory), // factory callback
};
@injectable()
export class Client {
constructor(@inject("SuperService") private service: SuperService) {}
}
container.register("SuperService", {
useClass: TestService
});
const client = container.resolve(Client);
export default
@withCookies
@withRouter
@connect(
state => {
return {
token: state.auth.token,
currentUser: state.auth.currentUser
};
},