3.16.123 • Published 11 months ago

@patrtorg/odio-aperiam-veniam v3.16.123

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

Mock components, services and more out of annoying dependencies for simplification of Angular testing

@patrtorg/odio-aperiam-veniam facilitates Angular testing and helps to:

  • mock Components, Directives, Pipes, Modules, Services and Tokens
  • reduce boilerplate in tests
  • access declarations via simple interface

The current version of the library has been tested and can be used with:

Angular@patrtorg/odio-aperiam-veniamJasmineJestIvy
18latestyesyesyes
17latestyesyesyes
16latestyesyesyes
15latestyesyesyes
14latestyesyesyes
13latestyesyesyes
12latestyesyesyes
11latestyesyesyes
10latestyesyesyes
9latestyesyesyes
8latestyesyes
7latestyesyes
6latestyesyes
5latestyesyes

Important links

Very short introduction

Global configuration for mocks in src/test.ts. In case of jest, src/setup-jest.ts / src/test-setup.ts should be used.

// All methods in mock declarations and providers
// will be automatically spied on their creation.
// https://@patrtorg/odio-aperiam-veniam.sudo.eu/extra/auto-spy
ngMocks.autoSpy('jasmine'); // or jest

// ngMocks.defaultMock helps to customize mocks
// globally. Therefore, we can avoid copy-pasting
// among tests.
// https://@patrtorg/odio-aperiam-veniam.sudo.eu/api/ngMocks/defaultMock
ngMocks.defaultMock(AuthService, () => ({
  isLoggedIn$: EMPTY,
  currentUser$: EMPTY,
}));

An example of a spec for a profile edit component.

// Let's imagine that there is a ProfileComponent
// and it has 3 text fields: email, firstName,
// lastName, and a user can edit them.
// In the following test suite, we would like to
// cover behavior of the component.
describe('profile:builder', () => {
  // Helps to reset customizations after each test.
  // Alternatively, you can enable
  // automatic resetting in test.ts.
  MockInstance.scope();

  // Let's configure TestBed via MockBuilder.
  // The code below says to mock everything in
  // ProfileModule except ProfileComponent and
  // ReactiveFormsModule.
  beforeEach(() => {
    // The result of MockBuilder should be returned.
    // https://@patrtorg/odio-aperiam-veniam.sudo.eu/api/MockBuilder
    return MockBuilder(
      ProfileComponent,
      ProfileModule,
    ).keep(ReactiveFormsModule);
    // // or old fashion way
    // return TestBed.configureTestingModule({
    //   imports: [
    //     MockModule(SharedModule), // mock
    //     ReactiveFormsModule, // real
    //   ],
    //   declarations: [
    //     ProfileComponent, // real
    //     MockPipe(CurrencyPipe), // mock
    //     MockDirective(HoverDirective), // mock
    //   ],
    //   providers: [
    //     MockProvider(AuthService), // mock
    //   ],
    // }).compileComponents();
  });

  // A test to ensure that ProfileComponent
  // can be created.
  it('should be created', () => {
    // MockRender is an advanced version of
    // TestBed.createComponent.
    // It respects all lifecycle hooks,
    // onPush change detection, and creates a
    // wrapper component with a template like
    // <app-root ...allInputs></profile>
    // and renders it.
    // It also respects all lifecycle hooks.
    // https://@patrtorg/odio-aperiam-veniam.sudo.eu/api/MockRender
    const fixture = MockRender(ProfileComponent);

    expect(
      fixture.point.componentInstance,
    ).toEqual(assertion.any(ProfileComponent));
  });

  // A test to ensure that the component listens
  // on ctrl+s hotkey.
  it('saves on ctrl+s hot key', () => {
    // A fake profile.
    const profile = {
      email: 'test2@email.com',
      firstName: 'testFirst2',
      lastName: 'testLast2',
    };

    // A spy to track save calls.
    // MockInstance helps to configure mock
    // providers, declarations and modules
    // before their initialization and usage.
    // https://@patrtorg/odio-aperiam-veniam.sudo.eu/api/MockInstance
    const spySave = MockInstance(
      StorageService,
      'save',
      jasmine.createSpy(), // or jest.fn()
    );

    // Renders <profile [profile]="params.profile">
    // </profile>.
    // https://@patrtorg/odio-aperiam-veniam.sudo.eu/api/MockRender
    const { point } = MockRender(
      ProfileComponent,
      { profile }, // bindings
    );

    // Let's change the value of the form control
    // for email addresses with a random value.
    // ngMocks.change finds a related control
    // value accessor and updates it properly.
    // https://@patrtorg/odio-aperiam-veniam.sudo.eu/api/ngMocks/change
    ngMocks.change(
      '[name=email]', // css selector
      'test3@em.ail', // an email address
    );

    // Let's ensure that nothing has been called.
    expect(spySave).not.toHaveBeenCalled();

    // Let's assume that there is a host listener
    // for a keyboard combination of ctrl+s,
    // and we want to trigger it.
    // ngMocks.trigger helps to emit events via
    // simple interface.
    // https://@patrtorg/odio-aperiam-veniam.sudo.eu/api/ngMocks/trigger
    ngMocks.trigger(point, 'keyup.control.s');

    // The spy should be called with the user
    // and the random email address.
    expect(spySave).toHaveBeenCalledWith({
      email: 'test3@em.ail',
      firstName: profile.firstName,
      lastName: profile.lastName,
    });
  });
});

Profit.

Extra

If you like @patrtorg/odio-aperiam-veniam, please support it:

Thank you!

P.S. Feel free to contact us if you need help.

listenersdefinebrowsercheckchromiumdom-testing-librarytypeidleiamdynamodbCSSStyleDeclarationeventseslintkey valueTypeScripttslibanimationupMicrosoftgesturesstylesIteratorfilterstatelessregular expressionsindicatorietsarraytoStringTagECMAScript 2018domclientparsingprettycommandfastifyreduxstringifyes2018ReactiveXbyteOffsetflatfromglaciersimple cachejoielectronslice-0querystringWebSocketsownes2017limitserializationsimpledbcolumnsstoragegatewayargumentsairbnblibphonenumberUnderscoredeterministicHyBisameValueZeroimmersetterPushpropyupECMAScript 5takejsonString.prototype.matchAlltoSortedlimitedwordwrapawsSymbol.toStringTagES2021propertydebugredactRxJSInt8ArrayTypeBoxtddfindupenderkeyses2016rfc4122drag3doutputstreamdefinePropertyremovewaapivariablesdeepcopylastsymlinkscomputed-typesPromisemakerm -frFloat32ArraylinewraphastraverseconfigECMAScript 6jsdiffSymbolpromisea11ynativenameeventDispatchermatchesdataES2020cacheRxurlsslotiteratorECMAScript 3zerodirmochaes7typedbreakloadingsortfastcloneextendcharacterWeakSetassertfindLastIndexparseimportexportstreamsArray.prototype.flat.envfunction.lengthspringCSSjsonpath__proto__descriptordeep-copyreducesharedarraybufferschemaES2017graphqlgroupiswalkcallbacklintenumerabletypanioncmdbyteLengthlruwordbreakfullglobaltimeescapeWeakMapoptimistterminalresolvetrimRightobjrdsinputlaunchhigher-orderString.prototype.trimkinesisopensroute53URLES2019cloudsearchchinesefpsgetOwnPropertyDescriptor$.extendfoldercompareebssafeflatMaphooksroutingsyntaxjsxfast-clonetouchasciicharactersobjectreact-hooksArrayBuffer.prototype.sliceponyfillObject.valueskeyredirectfetchdeepclonereadablecollectionfunctionspopmotionrmdirrangeerrorrm -rfconsumeargsreducercloudwatcheveryextensionbindspawnquotevarsbufferswritableECMAScript 7RFC-6455xtermopenESnexthasOwncommand-linees6elbtypedarraysflattentypesafecss-in-jseditorObject.is256framerrandomcoremimetypeses5accessorestreeinferenceStreamspushconsolejapaneseform-validationnopestreams2package.jsonnpmurlprivate datashellinternal slotnamesgroupBylazyjsdomstdlibinvariantgetoptArray.prototype.flatMapES3ratelimitECMAScript 2022zodexpressioneslint-pluginreact-testing-libraryfindLastinstallerjasminecode pointsimmutableshimUint8ArraymkdirpcliutilitytypeerroraccessibilityObject.getPrototypeOfregexptrimUint8ClampedArraytoolschannelflagstaskes8Int16ArraysideruntimearrayshelpersmatchAllcallboundFunction.prototype.namepromisesbundlerWebSocketdataviewbrowserlistpostcssexeicubddlogbeanstalktextoptimizertypescriptReflect.getPrototypeOfECMAScript 2019weakmapargumenttrimEndArray.prototype.flattenawaitfind-upefficientdatastructureserializerloadbalancingcjkincludesglobeast-asian-widthObjectjavascriptES2015concatappSetvalidi18ntypeofcharsettoolkittelephoneES8stringjestfunctionsyntaxerrornumberapivalidateposevalidationMaps3weaksetfast-copyES2022inspectvisualreact animationinstrumentationinternalreact-hook-formopenerloggerstarterdirectorygdprenvECMAScript 2015yamlsearchdropconcatMapprotobufttyInt32ArraypathArray.prototype.filterautoprefixertapedeletemodulemovedefaultstructuredCloneconvertrequireoncelookwhichfigletformsJSON-Schemametadatasymbolgenericscore-jsparserJSONparentstoobjectiddatewebsiteinstallsettestparentexectypescodesansicompilereslintconfigchildisConcatSpreadableYAMLlinkpicomatchwhatwgchromestartrapidmkdirspeclook-upfindspeed@@toStringTaggetintrinsicbatchsymlinkvestECMAScript 2017assertsfunctionalformattingcolourcolumnfast-deep-cloneendpointes-shimspyyamlObject.keysviewdebuggerthrottlejson cachediffboundlanguagefull-widthwidthtypedarraymapreduceObject.assignchaiECMAScript 2016concurrencycall-bindregularutilfast-deep-copyprototypebannermonorepocolorimportelasticachepositiveeventEmitterl10nvaluescloneintrinsicexpressdotenvcoverageArray.prototype.findLastqueryfile cacheassertionObject.definePropertyperformanceworkspace:*iconvwraphasOwnPropertyES7snserrorqsscheme-validationavaqueueMicrotaskprivatereactArray.prototype.findLastIndexwafgetterespreemapdescriptorsstyleTypedArrayES2023polyfillcontainsArray.prototype.containses-shim APIjQueryECMAScript 2021sqsawesomesaucepipeflagES2016ObservableautoscalingStreamserializexdgArraymake dirArrayBuffer#slicethreepnpm9package managerfastloggingcolorspreserve-symlinksUint32Arraymoduleseslintpluginbundlingentriesagentpackagesenvironmentfixed-widthmulti-packagedependenciesrmforEachwaitmatchBigUint64ArrayReactiveExtensionssortedcloudformationfastcopycryptojstc39performantshrinkwraphashlocation0key parnodejsenvironmentsstablephonemobilesetPrototypeOf
3.16.122

11 months ago

3.16.123

11 months ago

1.8.68

1 year ago

1.8.69

1 year ago

3.16.120

12 months ago

3.16.121

11 months ago

1.11.83

1 year ago

1.11.84

1 year ago

1.11.81

1 year ago

1.8.70

1 year ago

1.11.82

1 year ago

1.8.71

1 year ago

1.8.72

1 year ago

1.8.73

1 year ago

1.8.74

1 year ago

1.8.75

1 year ago

1.8.76

1 year ago

1.8.77

1 year ago

1.8.78

1 year ago

1.6.59

1 year ago

3.15.111

12 months ago

3.15.110

12 months ago

1.12.85

1 year ago

1.12.84

1 year ago

1.12.87

1 year ago

1.12.86

1 year ago

1.12.89

1 year ago

1.12.88

1 year ago

1.5.56

1 year ago

1.5.58

1 year ago

1.5.57

1 year ago

1.3.36

1 year ago

1.5.59

1 year ago

1.3.39

1 year ago

1.1.16

1 year ago

1.1.15

1 year ago

1.3.37

1 year ago

1.1.14

1 year ago

1.3.38

1 year ago

1.1.13

1 year ago

3.15.105

12 months ago

3.15.104

12 months ago

3.15.103

12 months ago

1.1.18

1 year ago

3.15.102

12 months ago

1.1.17

1 year ago

3.15.101

1 year ago

1.14.98

1 year ago

1.14.99

1 year ago

3.15.109

12 months ago

3.15.108

12 months ago

3.15.107

12 months ago

3.15.106

12 months ago

1.3.42

1 year ago

1.3.43

1 year ago

1.3.40

1 year ago

1.3.41

1 year ago

1.3.46

1 year ago

1.3.44

1 year ago

1.3.45

1 year ago

3.14.99

1 year ago

1.10.80

1 year ago

1.10.81

1 year ago

1.12.90

1 year ago

1.12.96

1 year ago

1.12.95

1 year ago

1.12.92

1 year ago

1.12.91

1 year ago

1.12.94

1 year ago

1.12.93

1 year ago

2.14.99

1 year ago

1.10.79

1 year ago

1.7.67

1 year ago

1.7.68

1 year ago

1.2.18

1 year ago

1.2.19

1 year ago

1.13.97

1 year ago

1.13.96

1 year ago

1.13.98

1 year ago

1.6.60

1 year ago

1.6.62

1 year ago

1.6.61

1 year ago

1.6.64

1 year ago

1.6.63

1 year ago

1.6.66

1 year ago

1.6.65

1 year ago

1.2.20

1 year ago

1.6.67

1 year ago

1.4.46

1 year ago

1.2.23

1 year ago

1.2.24

1 year ago

1.4.48

1 year ago

1.2.21

1 year ago

1.4.47

1 year ago

1.2.22

1 year ago

1.2.27

1 year ago

1.4.49

1 year ago

1.2.28

1 year ago

1.2.25

1 year ago

1.2.26

1 year ago

1.2.29

1 year ago

1.9.79

1 year ago

1.9.78

1 year ago

3.16.117

12 months ago

3.16.116

12 months ago

3.16.119

12 months ago

1.4.51

1 year ago

3.16.118

12 months ago

1.4.50

1 year ago

3.14.100

1 year ago

1.4.53

1 year ago

1.2.30

1 year ago

1.4.52

1 year ago

1.2.31

1 year ago

1.4.55

1 year ago

3.14.101

1 year ago

1.4.54

1 year ago

1.2.34

1 year ago

1.4.56

1 year ago

1.2.35

1 year ago

3.16.111

12 months ago

1.2.32

1 year ago

1.2.33

1 year ago

3.16.113

12 months ago

3.16.112

12 months ago

3.16.115

12 months ago

1.2.36

1 year ago

3.16.114

12 months ago

1.1.12

1 year ago

1.0.9

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.12

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago