5.13.113 • Published 10 months ago

@kollorg/quaerat-dolorem-soluta v5.13.113

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

@kollorg/quaerat-dolorem-soluta 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@kollorg/quaerat-dolorem-solutaJasmineJestIvy
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://@kollorg/quaerat-dolorem-soluta.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://@kollorg/quaerat-dolorem-soluta.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://@kollorg/quaerat-dolorem-soluta.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://@kollorg/quaerat-dolorem-soluta.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://@kollorg/quaerat-dolorem-soluta.sudo.eu/api/MockInstance
    const spySave = MockInstance(
      StorageService,
      'save',
      jasmine.createSpy(), // or jest.fn()
    );

    // Renders <profile [profile]="params.profile">
    // </profile>.
    // https://@kollorg/quaerat-dolorem-soluta.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://@kollorg/quaerat-dolorem-soluta.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://@kollorg/quaerat-dolorem-soluta.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 @kollorg/quaerat-dolorem-soluta, please support it:

Thank you!

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

pipejwtkeysdeepreact-hook-formstringifierhigher-orderstyleguidettyelasticachescheme-validationreducerAsyncIteratorimmutabletoSortedrandomzeroCSSbatchparserArrayBufferfluxstreams2concurrencymake dirlintiamcollectionexecparentcheckvestargsinternal slotspinnerInt8ArrayhttpdependenciesisConcatSpreadableliveschemestylingECMAScript 2022openbrowserslistreduxoncetestereslintpluginargparsedatainstallermapreducepredictableuuidclassescensorinvariantwgetoptimisttermES2015assertgetdynamodbgenericsdescriptorhasOwnflatmochaclimakewatchFilecss variablenope256codespostcssairbnbfile systemES8writablerapiderrorlrufunctionmimetypeslimitedECMAScript 2023CSSStyleDeclarationfindObject.valuescomputed-typesenderUint8ClampedArrayES2022uninstallObjecttoolssortedmkdirsefficientStreamsless mixinspreserve-symlinkseslintconfigwhatwgreadablestreamcolourArray.prototype.filtercjktostringtagecmascriptregular expressionsbundlersomeeverysameValueZerovisuallocationObject.getPrototypeOfArray.prototype.includesbusyinredux-toolkitArrayBuffer.prototype.slicegetoptvalidbreakmatchhasstoragegatewayloggingpruneclienttapeESsafebootstrap lessECMAScript 2016avajsonpathinstallpositiveES2016Object.fromEntriestddajaxString.prototype.matchAllthrottlewafunicodeWeakMapSetcallboundgradients cssutilpromisecorspostcss-pluginArray.prototype.flatMapbuffersStyleSheetsymbolcolumncall-boundMapmaparrayjasmineArrayforEachasynccloudwatchsearchtypeofresolveTypedArrayi18nbluebirdes-shim APIsettertestwalkjsnumberobjpreprocessorregexeventslistenersinferencemixinsfast-deep-clonedebugcoercibleECMAScript 5stylesdataviewlanguagecloneqsautoscalinghooksObject.assignroutingfastclonetraversesesreadablejsonstartermomentfast-clonedependency managercompareremovees5route53ES2019valueextensionassertsreact-hookslockfilejsxtypeerrorcallbindstatelesspatchBigInt64ArrayUint32Arrayoptimizerdatesortpnpm9compileres-shimstypedarraychromiumbrowsersetImmediateES2018sqscontainsinputwatchimmerwaitlazyimportexportwidthbcryptbrowserlisttaskgetintrinsicemojiArray.prototype.flattenES5superstructl10ncopyvariables in cssviewmoduleloggerupshellArray.prototype.flatwrapidlestringifytapexpressstatearktypequoteWebSocketshookformReflect.getPrototypeOfsimpledbbindfsshebangratelimitmatchAlls3Uint16ArrayaccessordomnodeObject.definePropertyhasOwnPropertytoStringTaggetterslotdiffredactreactec2internalsymlinksmobileenvtypedarrayscall-bindfindupapolloreal-time.envcryptrdsconfigcloudformationfunctionaldeterministicpropflagelectrontypesafewordwrapkinesisrequestendpoint[[Prototype]]gradients css3parsingroutecss nestingiteratorruntimetrimStartauthjapanesedatastructureless compilerstringFloat64ArrayconcatMapWebSocketlogjQuerybddnested cssajvphoneJSON-Schemaworkspace:*httpscssurlcommand-lineArray.prototype.containsdeepcloneratetypeInt32Arraynegativesequenceiterationiteratefiltercore-jssnspushgroupgraphqlBigUint64Arraywritermdirmimeprotocol-buffersboundarraysframeworkispersistentfigletinspectdeep-cloneglacierstdlibES2017lessRegExp#flagsarraybuffertrimRightString.prototype.trimdayjsprotoglobbyteOffsetasciiweakmapformatwalkingvalidationserializeregular expressionsettingstrimbyteLengthreplay__proto__emrsetPrototypeOfplugincode pointsbuffermatchesmetadatarm -rfclass-validatorproxyrangeerrorconsumehelpersmodulesrmapilengthponyfillgdprstreamsauthenticationbundlingworkflowprotobuffunctionsPushwebperformantSymbolxtermexpressionthroatArray.prototype.findLastschemargb
5.13.113

10 months ago

5.13.110

10 months ago

5.13.112

10 months ago

5.13.111

10 months ago

5.12.110

10 months ago

5.12.109

10 months ago

5.12.108

11 months ago

5.12.107

11 months ago

5.12.106

11 months ago

5.12.105

11 months ago

5.12.104

11 months ago

5.12.103

11 months ago

5.12.102

11 months ago

5.12.101

11 months ago

5.12.100

11 months ago

5.11.100

11 months ago

4.10.87

11 months ago

3.2.24

1 year ago

4.10.88

11 months ago

3.2.23

1 year ago

4.10.89

11 months ago

3.2.26

1 year ago

3.2.25

1 year ago

4.10.83

11 months ago

3.2.28

1 year ago

4.10.84

11 months ago

3.2.27

1 year ago

4.10.85

11 months ago

4.10.86

11 months ago

3.2.29

1 year ago

3.2.20

1 year ago

3.2.22

1 year ago

3.2.21

1 year ago

4.10.82

11 months ago

4.5.61

1 year ago

4.5.62

1 year ago

3.2.30

1 year ago

4.5.65

1 year ago

4.5.66

1 year ago

4.5.63

1 year ago

4.5.64

1 year ago

4.5.67

12 months ago

4.8.81

12 months ago

4.8.80

12 months ago

4.8.82

12 months ago

4.4.43

1 year ago

4.6.67

12 months ago

4.4.42

1 year ago

4.4.41

1 year ago

4.4.40

1 year ago

4.4.47

1 year ago

4.4.46

1 year ago

4.6.68

12 months ago

4.4.45

1 year ago

4.6.69

12 months ago

4.4.44

1 year ago

4.4.49

1 year ago

4.4.48

1 year ago

3.2.15

1 year ago

3.2.14

1 year ago

3.2.17

1 year ago

3.2.16

1 year ago

3.2.19

1 year ago

3.2.18

1 year ago

4.8.76

12 months ago

4.8.75

12 months ago

4.8.78

12 months ago

4.8.77

12 months ago

4.8.79

12 months ago

4.4.39

1 year ago

4.11.98

11 months ago

4.11.97

11 months ago

4.11.96

11 months ago

4.11.99

11 months ago

3.3.30

1 year ago

4.4.61

1 year ago

3.3.31

1 year ago

4.4.60

1 year ago

3.3.32

1 year ago

4.6.70

12 months ago

4.6.73

12 months ago

4.4.50

1 year ago

4.6.74

12 months ago

4.6.71

12 months ago

4.6.72

12 months ago

4.4.54

1 year ago

4.4.53

1 year ago

4.4.52

1 year ago

4.4.51

1 year ago

4.4.58

1 year ago

4.4.57

1 year ago

4.4.56

1 year ago

4.4.55

1 year ago

5.11.99

11 months ago

4.4.59

1 year ago

4.7.74

12 months ago

4.7.75

12 months ago

4.3.35

1 year ago

4.3.34

1 year ago

4.3.33

1 year ago

4.3.32

1 year ago

4.3.39

1 year ago

4.3.38

1 year ago

4.3.37

1 year ago

4.3.36

1 year ago

4.10.94

11 months ago

4.10.95

11 months ago

4.10.96

11 months ago

4.9.82

12 months ago

4.10.90

11 months ago

4.10.91

11 months ago

4.10.92

11 months ago

4.10.93

11 months ago

3.2.13

1 year ago

3.2.12

1 year ago

3.1.11

1 year ago

3.2.11

1 year ago

3.1.10

1 year ago

2.1.10

1 year ago

2.1.9

1 year ago

2.0.9

1 year ago

1.0.9

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.2

1 year ago

1.0.3

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago