aboutsummaryrefslogtreecommitdiff
path: root/test/internal/bind-this-to-member-functions.spec.ts
blob: 93b0a9cbf42d40688e0fb6ff5d93f4bba63d612f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import { describe, expect, it } from 'vitest';
import type { Faker } from '../../src';
import { faker } from '../../src';
import { bindThisToMemberFunctions } from '../../src/internal/bind-this-to-member-functions';

describe('internal', () => {
  describe('bind-this-to-member-functions', () => {
    it('should bind this to member functions', () => {
      class SomeModule {
        constructor(private readonly faker: Faker) {}

        someMethod(): number {
          return this.faker.number.int();
        }
      }

      const someModule = new SomeModule(faker);

      const someMethodWithoutBind = someModule.someMethod;

      expect(() => someMethodWithoutBind()).toThrow(
        new TypeError("Cannot read properties of undefined (reading 'faker')")
      );

      bindThisToMemberFunctions(someModule);

      const someMethod = someModule.someMethod;

      expect(() => someMethod()).not.toThrow();
    });
  });
});