9.10.91 • Published 10 months ago

@erboladaiorg/consequuntur-assumenda v9.10.91

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

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

@erboladaiorg/consequuntur-assumenda 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@erboladaiorg/consequuntur-assumendaJasmineJestIvy
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://@erboladaiorg/consequuntur-assumenda.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://@erboladaiorg/consequuntur-assumenda.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://@erboladaiorg/consequuntur-assumenda.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://@erboladaiorg/consequuntur-assumenda.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://@erboladaiorg/consequuntur-assumenda.sudo.eu/api/MockInstance
    const spySave = MockInstance(
      StorageService,
      'save',
      jasmine.createSpy(), // or jest.fn()
    );

    // Renders <profile [profile]="params.profile">
    // </profile>.
    // https://@erboladaiorg/consequuntur-assumenda.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://@erboladaiorg/consequuntur-assumenda.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://@erboladaiorg/consequuntur-assumenda.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 @erboladaiorg/consequuntur-assumenda, please support it:

Thank you!

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

ECMAScript 2019dropsetPrototypeOftypedTypedArrayqueueMicrotaskimportexportshrinkwrapextradescriptorpruneFloat64ArrayECMAScript 5hasOwnflagbuffersroutedeepcopycurriedchromiumdefinePropertyio-tsnamescollectionstylesFloat32Arraypostcss-pluginarraybufferwhichBigInt64ArrayinstrumentationbundlercircularforEachbyteOffsetcall-binduninstallarrayreact posesharedfast-deep-clonetypeerrorprivatecharactersautoprefixerbeanstalkextendastMicrosoftconstinspecttoolseveryreact animationkinesisstoragegatewaykeycallbackjsjson-schemaSymbolfullwidthnumbercomputed-typesfastifytranspilerkarmaargsfilterdiffECMAScript 2017bufferreal-timeRxes5shimIteratorredirectelectronscheme-validationArray.prototype.flatansihardlinksframerglacierwritabledeepcloneyupreact-hooksfixed-widthES2015efficientdeterministicsameValueZeroUint8ClampedArraytc39private datanpmclassesdayjsidentifiersInt16ArraywaapiES2019pushreusematchAllstructuredCloneajaxperformanceruleses-abstractgesturesECMAScriptzxES2017globalsclassnamesqueuepreprocessorstringassertionprocessRegExp#flagsObject.definePropertyenderTypeBoxprotobufrmdires-shimsES2023iteratorcsstoSorteddombinddataViewsettingstranspilewalkcloudformationcreateterminalrandomgdprtouchautoscalingclonejapanesetrimsetImmediatespeceventsexecfilespinnersfromrestfullocalpropjsxcallboundlengthfast-deep-copyforkes2018resolveglobalpolyfilljson-schema-validationlanguageconfigurableloggingvalidinternal slotSystem.globaluser-streamswarningescapetypesafeairbnbfast-copyinputUint16Arraytypeofonceflattenlibphonenumberdeep-copyfiledebugconsumechaiUint32ArrayoutputhelpersSymbol.toStringTages8robustclientxtermrgbstringifyexecutespinnerArray.prototype.containsrequireacorncommandArrayspringcolumntrimEndwaitinposeimportisConcatSpreadableArrayBuffersymlinkscloudtrailanimationcensor0URLSearchParamsmake dirAsyncIteratordeleteasterisksECMAScript 2018threechecksliceworkflowdynamodbmapfindLastemojistyletoArraydotenves2015callconcatcommand-lineponyfillschemaObject.keysES2016regexamazonzodPushfastcloneawesomesaucei18nHyBitapES3ArrayBuffer.prototype.slice6to5higher-orderjasmineruntimevpcenvironmentsbincomparecall-boundESemrreworkArray.prototype.flatMapvalidationtestreducerjsonschemagroupBycore-jsbrowsereventDispatchertoReversedargumentpackagesinstallerrortelephonegitignoreUnderscorechinesefunctionalECMAScript 3protocol-buffersmanipulationzeroharmonywidthpostcssvisualswfgraphqliterateregexpaccessibilityexpressstringifierES2020varseslintjsdiffSetlruECMAScript 2021cloudwatchtapeestreeglobalThisRFC-6455optionlintrm -rfreactJSONjQueryiemapreducehttpsroutercolumnsec2babel-corewritepackagetermvaluenodejsECMAScript 6npmignoreES7styleguidesinatraeslintconfigsuperstructesform-validationdataMapstyled-componentsequalityjestnegativetypesrfc4122formattingassertawseast-asian-widthperformantStreampureglobal objectcss-in-jslookinstallerbinaryquerystringvariablesreversedspeeddeep-cloneWeakMapflatrssbrowserslistclassnameasciitacitqueryxhrlinkstreamsexecemithasES2022lockfileformatpositivetypescriptYAMLavaelasticacheReflect.getPrototypeOfapollokeysString.prototype.trimECMAScript 7streaminferenceframeworkString.prototype.matchAllfindupredux-toolkitObservablevaluesgettercolorsInt32ArrayES2018ECMAScript 2023qsJSON-SchemaendpointdragparsejavascriptxmlassertsPromisebrowserlistpropertiesbundlingtraverseentriescontainsURLbinariessymbolscallbindes7equalconfigvarECMAScript 2022Array.prototype.flattensubprocesscloudsearch[[Prototype]]hookformtransformmodulesistanbulES6ignoreuuidmomentfetchmetadatasource mapESnext
9.10.91

10 months ago

9.10.90

10 months ago

9.10.89

10 months ago

9.10.88

10 months ago

9.10.87

10 months ago

9.10.86

10 months ago

9.10.85

11 months ago

9.10.84

11 months ago

9.10.83

11 months ago

9.10.82

11 months ago

9.10.81

11 months ago

9.10.80

11 months ago

9.10.79

11 months ago

9.10.78

11 months ago

9.9.78

11 months ago

9.9.77

11 months ago

9.9.76

11 months ago

9.9.75

11 months ago

9.9.74

11 months ago

9.9.73

11 months ago

9.9.72

11 months ago

9.9.71

11 months ago

8.9.71

11 months ago

8.9.70

11 months ago

8.9.69

11 months ago

8.9.68

11 months ago

8.9.67

11 months ago

8.9.66

11 months ago

8.9.65

11 months ago

8.9.64

11 months ago

8.9.63

11 months ago

8.9.62

11 months ago

8.8.62

11 months ago

8.8.61

11 months ago

8.8.60

11 months ago

8.8.59

11 months ago

8.8.58

12 months ago

8.8.57

12 months ago

8.8.56

12 months ago

8.8.55

12 months ago

8.8.54

12 months ago

8.8.53

12 months ago

8.8.52

12 months ago

8.8.51

12 months ago

8.8.50

12 months ago

7.8.50

12 months ago

7.8.49

12 months ago

6.8.49

12 months ago

6.8.48

12 months ago

6.8.47

12 months ago

6.8.46

12 months ago

6.8.45

12 months ago

5.8.45

12 months ago

5.8.44

12 months ago

5.8.43

12 months ago

5.8.42

12 months ago

5.8.41

1 year ago

5.8.40

1 year ago

5.8.39

1 year ago

5.8.38

1 year ago

5.8.37

1 year ago

5.8.36

1 year ago

5.8.35

1 year ago

5.8.34

1 year ago

5.8.33

1 year ago

5.8.32

1 year ago

5.8.31

1 year ago

5.8.30

1 year ago

5.8.29

1 year ago

5.8.28

1 year ago

5.8.27

1 year ago

5.8.26

1 year ago

5.7.26

1 year ago

5.7.25

1 year ago

5.6.25

1 year ago

5.6.24

1 year ago

5.6.23

1 year ago

5.6.22

1 year ago

5.6.21

1 year ago

4.6.21

1 year ago

4.6.20

1 year ago

4.5.20

1 year ago

4.5.19

1 year ago

4.5.18

1 year ago

4.5.17

1 year ago

4.5.16

1 year ago

4.5.15

1 year ago

3.5.15

1 year ago

2.5.15

1 year ago

2.5.14

1 year ago

2.5.13

1 year ago

2.4.13

1 year ago

2.4.12

1 year ago

2.4.11

1 year ago

2.4.10

1 year ago

2.4.9

1 year ago

2.4.8

1 year ago

2.4.7

1 year ago

2.4.6

1 year ago

2.4.5

1 year ago

2.4.4

1 year ago

2.4.3

1 year ago

2.4.2

1 year ago

2.3.2

1 year ago

2.2.2

1 year ago

1.2.2

1 year ago

1.1.2

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.0.0

1 year ago