npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tsyringe-react

v0.1.0

Published

A way to implement tsyringe together with react hooks, use it if you need dependency injection without interfaces or with a small container.

Downloads

4

Readme

Tsyringe React

A way to implement tsyringe together with react hooks, use it if you need dependency injection without interfaces or with a small container.

Installation

First you have to install tsyringe

  • npm install --save tsyringe-react
  • yarn add tsyringe-react

´withInjection´ helper

A helper for register the function with the token and wrap the function with a container.resolver.

  1. Wrap React hook with ´withInjection´ helper.

    // use-calc.ts
    import { withInjection } 'tsyringe-react'
    
    const useCalc = (n1: number, n2: number): number => {
      return n1 + n2;
    }
    
    export default withInjection('useCalc', useCalc) // add withInjection
    
  2. Use the wrapped hook

    // CalcCard.tsx
    import useCalc from 'use-calc';
    import { FC } from 'react';
    import { useResolve } from 'tsyringe-react';
    
    type CalcCardProps = {
      number1: number;
      number2: number;
    };
    
    const CalcCard: FC<CalcCardProps> = ({ number1, number2 }) => {
      const result = useCalc(number1, number2);
    
      return <div>Result : {result}</div>;
    };
    
    export default Calc;
  3. inject mock in tests

// Calc.test.tsx
import { render, screen } from '@testing-library/react';
import CalcCard from 'CalcCard';

test('should render result', () => {
  const number1 = 1;
  const number2 = 2;
  const result = 3;

  const mockUseCalc = jest.fn(() => result);

  // inject mock
  container.register('useCalc', {
    useValue: mockUseCalc,
  });

  const { container } = render(
    <CalcCard number1={number1} number2={number2} />
  );

  expect(container).toHaveTextContent('Result : 3');
  expect(mockUseCalc).toBeCalledWith(number1, number2);
});

useResolve hook

1.- create container

// it is very important that the container import it from `tsyringe-react`
import { container } from 'tsyringe-react';
import { API_TOKEN } from 'constants';

container.register(API_TOKEN, { useFactory: () => process.env.API_TOKEN });

2.- create some class

// get-user.service.ts
import { User } from './user.entity';
import { injectable } from 'tsyringe-react';

@injectable
export class GetUserService {
  perform(apiToken: string): User {
    //...
  }
}

3.- Use useResolve hook

// UserCard.tsx
import { API_TOKEN } from './constants';
import { GetUserService } from './get-user.service';

const UserCard: FC<CalcCardProps> = ({ number1, number2 }) => {
  // The token can be a string
  const apiToken = useResolve<string>(API_TOKEN);

  // The token can be a class, in which case `useResolve` returns an instance of that class
  const getUserService = useResolve<string>(API_TOKEN);

  const user = getUserService.perform(apiToken);

  return <div>user name : {user.name}</div>;
};

export default UserCard;
  1. inject mock in tests

Note: in order not to conflict with the types, I use jest-mock-extended to create the service mock

// UserCard.test.tsx
import { mock } from 'jest-mock-extended';
import { render, screen } from '@testing-library/react';
import { API_TOKEN } from './constants';
import { GetUserService } from './get-user.service';

import UserCard from './UserCard';

test('should render user name', () => {
  const apiToken = '123';
  const user = new User('user one');
  const mockGetUserService = mock<GetUserService>();

  mockGetUserService.perform.mockReturnValue(user);

  // inject mocks
  container.register(API_TOKEN, { useValue: apiToken });
  container.register(GetUserService, { useValue: mockGetUserService });

  const { container } = render(<UserCard />);

  expect(container).toHaveTextContent('user name : user one);
  expect(mockGetUserService.perform).toBeCalledWith(apiToken);
});

Example project

You can see an example of a next project in the example directory

git clone [email protected]:ramon-sg/tsyringe-react.git
cd tsyringe-react/example
yarn install
yarn dev

// open http://localhost:3000

TODO

  • [ ] Improve the example project with more tests
  • [ ] A better RADME
  • [ ] a better english :(