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

@ngx-unit-test/inject-mocks

v1.1.1

Published

unit testing library for Angular framework

Downloads

373

Readme

@ngx-unit-test/inject-mocks

A utility library for injecting mocked dependencies in Angular

Installation

Install "@ngx-unit-test/inject-mocks" in your Angular project using the command:

npm i @ngx-unit-test/inject-mocks --save-dev

Usage

The "@ngx-unit-test/inject-mocks" library provides two utility functions - classWithProviders and runFnInContext - that make it easy to inject mocked values into Angular classes and functions.

Testing Classes: classWithProviders

The classWithProviders utility function injects mocked providers into a given class. To use this function, pass in a configuration object with a token property set to the target class and a providers property set to an array of mocked providers that you want to inject into the class. The classWithProviders function then automatically injects these mocks into the class and returns the modified class with the dependencies replaced by the mocked providers.

Here is an example:

import { inject, Injectable } from '@angular/core';

@Injectable({ providedIn: 'root' })
export class EmojiService {
  emoji = '🐧';
}
import { Component, inject } from '@angular/core';
import { EmojiService } from './emoji.service';

@Component({
  selector: 'ngx-unit-test-emoji',
  template: ` <p>Tweet Tweet {{ emoji }}</p> `,
})
export class EmojiComponent implements OnInit {
  readonly emoji = inject(EmojiService).emoji;
}
import { classWithProviders } from '@ngx-test/inject-mocks';

it('should inject mocks into a Component', () => {
  // Arrange
  const emoji = '🐦';
  const serviceMock: Partial<EmojiService> = { emoji };
  const component = classWithProviders({
    token: EmojiComponent,
    providers: [{ provide: EmojiService, useValue: serviceMock }],
  });
  // Act
  component.ngOnInit();
  // Assert
  expect(component.emoji).toBe(emoji);
});

The classWithProviders utility can be used with Components, Directives, Pipes, Services, and any other Angular class that can use the inject function. It also works with classes that use constructor-based dependency injection.

Testing Functions: runFnInContext

The runFnInContext function takes an array of mocked providers, and executes the function with the specified providers injected.

Here is an example:

import { HttpClient } from '@angular/common/http';
import { inject } from '@angular/core';

export function injectHttpClient(): HttpClient {
  return inject(HttpClient);
}
import { HttpClient } from '@angular/common/http';
import { runFnInContext } from '@ngx-test/inject-mocks';
import { injectHttpClient } from './inject-http-client';

describe('injectHttpClient()', () => {
  let httpMock: Partial<HttpClient>;
  let httpClient: HttpClient;

  beforeEach(() => {
    httpMock = { get: jest.fn() };
    const providers = [{ provide: HttpClient, useValue: httpMock }];
    httpClient = runFnInContext(providers, () => injectHttpClient());
  });

  it('should inject HttpClient', () => {
    expect(httpClient).toBeTruthy;
  });

  it('should invoke http.get', () => {
    // Arrange
    const mockUrl = 'mockUrl';
    const spy = jest.spyOn(httpMock, 'get');
    //Act
    httpClient.get(mockUrl);
    // Assert
    expect(spy).toBeCalledWith(mockUrl);
  });
});