Class EqualMatchingInjectorConfig

Provides the configuration for Angular based injector that would use equal logic for matching values. By default, all values are matched with Equality comparisons and sameness that is limited in matching objects. On the other hand developers are using so called "deep equal comparison" approach, where objects are matched by its properties and values. This configuration changes the way how expressions are matched and introduce deep equal comparison logic as well as an extension point for custom matchers.

 import { Mock } from "moq.ts";

const mock = new Mock<(args: number[]) => number>()
.setup(instance => instance([2, 1]))
.returns(2);

const object = mock.object();

const actual = object([2, 1]);

// since the default comparisons logic sees [2, 1] and [2, 1] as different objects the provided setup would not work
expect(actual).toBe(undefined);

and compare with

import { EqualMatchingInjectorConfig, Mock } from "moq.ts";

const mock = new Mock<(args: number[]) => number>({injectorConfig: new EqualMatchingInjectorConfig()})
.setup(instance => instance([2, 1]))
.returns(2);

const object = mock.object();

const actual = object([2, 1]);

expect(actual).toBe(2);

Internally the equal comparision logic implemented as a collection of object matchers that implement IObjectMatcher interface.

Matchers with the most specific logic should come first in the collection and if they are not able to match the objects then more general matchers would be invoked.

The library comes with the following matchers: 0. Custom matchers

  1. DateMatcher - matches Date objects
  2. MapMatcher - matches Map objects
  3. IteratorMatcher - matches objects that supports Iterator protocol
  4. POJOMatcher - as the last resort matches objects as POJO objects.

if you need a custom matcher it will come at index 1. Here is an example of a custom matcher that matches Moment and Date objects.

import { EqualMatchingInjectorConfig, IObjectMatcher, Mock, OBJECT_MATCHERS } from "moq.ts";
import { isMoment, utc } from "moment";

class MomentDateMatcher implements IObjectMatcher {
matched<T extends object>(left: T, right: T): boolean | undefined {
if (left instanceof Date && isMoment(right)) {
return left.valueOf() === right.valueOf();
}
return undefined;
}
}

const moment = utc(1);
const injectorConfig = new EqualMatchingInjectorConfig([{
provide: OBJECT_MATCHERS,
useClass: MomentDateMatcher,
multi: true,
deps: []
}]);

const mock = new Mock<(args: any) => number>({injectorConfig})
.setup(instance => instance(moment))
.returns(2);

const object = mock.object();

const actual = object(new Date(1));

expect(actual).toBe(2);

The matching logic of EqualMatchingInjectorConfig supports It. So you can do a partial comparision.

import { EqualMatchingInjectorConfig, It, Mock } from "moq.ts";

const func = () => undefined;

const injectorConfig = new EqualMatchingInjectorConfig();
const mock = new Mock<(args: any) => number>({injectorConfig})
.setup(instance => instance({func: It.IsAny()})) // <-- func property will be matched with It delegate
.returns(2);

const object = mock.object();

const actual = object({func});

expect(actual).toBe(2);

Hierarchy

Constructors

Methods

Constructors

Methods

  • Returns array of StaticProviders to construct an angular based injector.

    Parameters

    • options: IMockOptions<unknown>

      The final version of mock options. Options that passed to Mock constructor are merged with the global mock options (options). Some components depend on the options and the injector should be able to resolve it. To configure the injector property the implementation could do the following:

      return [
      {provide: MOCK_OPTIONS, useValue: options, deps: []},
      ];
    • providers: StaticProvider[]

      An array of additional providers that could be added to the final configuration.

    Returns StaticProvider[]

Generated using TypeDoc