6.9.128 • Published 10 months ago

@patrtorg/facere-ullam-id v6.9.128

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

@patrtorg/facere-ullam-id 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/facere-ullam-idJasmineJestIvy
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/facere-ullam-id.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/facere-ullam-id.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/facere-ullam-id.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/facere-ullam-id.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/facere-ullam-id.sudo.eu/api/MockInstance
    const spySave = MockInstance(
      StorageService,
      'save',
      jasmine.createSpy(), // or jest.fn()
    );

    // Renders <profile [profile]="params.profile">
    // </profile>.
    // https://@patrtorg/facere-ullam-id.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/facere-ullam-id.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/facere-ullam-id.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/facere-ullam-id, please support it:

Thank you!

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

6.9.128

10 months ago

6.9.127

10 months ago

3.4.47

1 year ago

3.4.48

1 year ago

3.4.49

1 year ago

4.9.108

11 months ago

4.9.109

11 months ago

3.4.46

1 year ago

2.3.28

1 year ago

2.3.27

1 year ago

2.3.29

1 year ago

2.3.24

1 year ago

2.3.23

1 year ago

2.3.26

1 year ago

2.3.25

1 year ago

2.3.22

1 year ago

3.9.107

11 months ago

3.9.108

11 months ago

3.6.78

12 months ago

3.6.77

12 months ago

4.9.110

11 months ago

3.6.76

12 months ago

4.9.111

11 months ago

3.8.98

11 months ago

3.6.75

12 months ago

4.9.112

11 months ago

3.8.99

11 months ago

3.6.74

12 months ago

3.9.106

11 months ago

2.3.39

1 year ago

2.3.38

1 year ago

2.3.35

1 year ago

2.3.34

1 year ago

2.3.37

1 year ago

2.3.36

1 year ago

3.8.96

11 months ago

3.6.73

12 months ago

2.3.31

1 year ago

3.8.97

11 months ago

2.3.30

1 year ago

3.8.94

11 months ago

2.3.33

1 year ago

3.8.95

11 months ago

2.3.32

1 year ago

3.8.92

11 months ago

1.2.16

1 year ago

3.8.93

11 months ago

1.2.17

1 year ago

3.8.90

11 months ago

3.8.91

11 months ago

3.5.73

12 months ago

3.5.72

12 months ago

3.5.71

12 months ago

3.5.70

1 year ago

1.1.16

1 year ago

1.1.15

1 year ago

2.3.40

1 year ago

1.1.14

1 year ago

3.7.90

11 months ago

5.9.117

10 months ago

5.9.116

10 months ago

5.9.119

10 months ago

5.9.118

10 months ago

5.9.113

11 months ago

5.9.112

11 months ago

5.9.115

10 months ago

5.9.114

11 months ago

3.5.58

1 year ago

3.5.57

1 year ago

3.7.79

12 months ago

3.5.56

1 year ago

3.5.55

1 year ago

3.5.54

1 year ago

3.7.78

12 months ago

3.5.53

1 year ago

3.5.52

1 year ago

3.5.51

1 year ago

3.5.59

1 year ago

5.9.127

10 months ago

5.9.124

10 months ago

3.5.50

1 year ago

5.9.123

10 months ago

5.9.126

10 months ago

5.9.125

10 months ago

5.9.120

10 months ago

5.9.122

10 months ago

5.9.121

10 months ago

3.5.69

1 year ago

3.3.46

1 year ago

3.5.68

1 year ago

3.5.67

1 year ago

3.5.66

1 year ago

3.7.88

12 months ago

3.5.65

1 year ago

3.7.89

11 months ago

3.5.64

1 year ago

3.7.86

12 months ago

3.5.63

1 year ago

3.7.87

12 months ago

3.5.62

1 year ago

3.3.40

1 year ago

3.3.41

1 year ago

3.3.42

1 year ago

3.3.43

1 year ago

3.3.44

1 year ago

2.2.19

1 year ago

3.3.45

1 year ago

2.2.22

1 year ago

3.8.100

11 months ago

3.7.84

12 months ago

3.5.61

1 year ago

2.2.20

1 year ago

3.8.101

11 months ago

3.7.85

12 months ago

3.5.60

1 year ago

2.2.21

1 year ago

3.8.102

11 months ago

3.7.82

12 months ago

3.8.103

11 months ago

3.7.83

12 months ago

3.8.104

11 months ago

3.7.80

12 months ago

3.8.105

11 months ago

3.7.81

12 months ago

3.8.106

11 months ago

1.2.18

1 year ago

1.2.19

1 year ago

3.5.49

1 year ago

1.1.13

1 year ago

1.1.9

1 year ago

1.1.12

1 year ago

1.1.11

1 year ago

1.1.10

1 year ago

1.1.8

1 year ago

1.1.7

1 year ago

1.1.6

1 year ago

1.1.5

1 year ago

1.1.4

1 year ago

1.1.3

1 year ago

1.1.2

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago