1.11.114 • Published 1 year ago

@devtea2026/cum-dolore-similique-beatae v1.11.114

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

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

@devtea2026/cum-dolore-similique-beatae 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@devtea2026/cum-dolore-similique-beataeJasmineJestIvy
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://@devtea2026/cum-dolore-similique-beatae.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://@devtea2026/cum-dolore-similique-beatae.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://@devtea2026/cum-dolore-similique-beatae.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://@devtea2026/cum-dolore-similique-beatae.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://@devtea2026/cum-dolore-similique-beatae.sudo.eu/api/MockInstance
    const spySave = MockInstance(
      StorageService,
      'save',
      jasmine.createSpy(), // or jest.fn()
    );

    // Renders <profile [profile]="params.profile">
    // </profile>.
    // https://@devtea2026/cum-dolore-similique-beatae.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://@devtea2026/cum-dolore-similique-beatae.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://@devtea2026/cum-dolore-similique-beatae.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 @devtea2026/cum-dolore-similique-beatae, please support it:

Thank you!

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

1.11.114

1 year ago

1.11.113

1 year ago

1.11.112

1 year ago

1.11.111

1 year ago

1.11.110

1 year ago

1.11.109

1 year ago

1.11.108

1 year ago

1.11.107

1 year ago

1.11.96

1 year ago

1.11.97

1 year ago

1.11.94

1 year ago

1.11.95

1 year ago

1.11.93

1 year ago

1.8.60

1 year ago

1.8.61

1 year ago

1.8.62

1 year ago

1.8.63

1 year ago

1.8.64

1 year ago

1.8.65

1 year ago

1.8.66

1 year ago

1.8.67

1 year ago

1.6.44

1 year ago

1.8.68

1 year ago

1.6.43

1 year ago

1.11.98

1 year ago

1.8.69

1 year ago

1.6.46

1 year ago

1.11.99

1 year ago

1.6.45

1 year ago

1.6.48

1 year ago

1.6.47

1 year ago

1.6.49

1 year ago

1.8.70

1 year ago

1.8.71

1 year ago

1.8.72

1 year ago

1.8.73

1 year ago

1.6.51

1 year ago

1.6.50

1 year ago

1.6.53

1 year ago

1.6.52

1 year ago

1.6.54

1 year ago

1.4.33

1 year ago

1.4.35

1 year ago

1.4.34

1 year ago

1.4.37

1 year ago

1.4.36

1 year ago

1.4.39

1 year ago

1.4.38

1 year ago

1.3.32

1 year ago

1.3.33

1 year ago

1.8.54

1 year ago

1.8.55

1 year ago

1.8.56

1 year ago

1.8.57

1 year ago

1.8.58

1 year ago

1.8.59

1 year ago

1.10.84

1 year ago

1.10.85

1 year ago

1.10.82

1 year ago

1.10.83

1 year ago

1.10.80

1 year ago

1.10.81

1 year ago

1.7.54

1 year ago

1.10.88

1 year ago

1.10.89

1 year ago

1.10.86

1 year ago

1.10.87

1 year ago

1.11.102

1 year ago

1.11.103

1 year ago

1.11.100

1 year ago

1.11.101

1 year ago

1.11.106

1 year ago

1.11.104

1 year ago

1.11.105

1 year ago

1.10.74

1 year ago

1.10.79

1 year ago

1.10.77

1 year ago

1.5.43

1 year ago

1.10.78

1 year ago

1.10.75

1 year ago

1.10.76

1 year ago

1.2.19

1 year ago

1.4.40

1 year ago

1.4.42

1 year ago

1.4.41

1 year ago

1.2.20

1 year ago

1.4.43

1 year ago

1.2.23

1 year ago

1.2.24

1 year ago

1.2.21

1 year ago

1.2.22

1 year ago

1.2.27

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

1 year ago

1.10.91

1 year ago

1.10.92

1 year ago

1.10.90

1 year ago

1.9.74

1 year ago

1.9.73

1 year ago

1.2.30

1 year ago

1.2.31

1 year ago

1.2.32

1 year ago

1.2.18

1 year ago

1.2.17

1 year ago

1.2.16

1 year ago

1.1.16

1 year ago

1.1.15

1 year ago

1.1.14

1 year ago

1.1.13

1 year ago

1.1.12

1 year ago

1.0.12

1 year ago

1.0.11

1 year ago

1.0.10

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

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago