• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

DethAriel / ng-recaptcha / 720

pending completion
720

cron

travis-ci-com

DethAriel
chore(demo): use invisible reCAPTCHA site key in the example

51 of 51 branches covered (100.0%)

Branch coverage included in aggregate %.

188 of 189 relevant lines covered (99.47%)

9.08 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.88
/projects/ng-recaptcha/src/lib/test-utils/mock-grecaptcha.ts
1
export type GrecaptchaMockedMethods = Record<keyof ReCaptchaV2.ReCaptcha, jasmine.Spy>;
2
export type MockGrecaptchaType = GrecaptchaMockedMethods & {
3
  enterprise: GrecaptchaMockedMethods;
4
};
5

6
export class MockGrecaptcha implements MockGrecaptchaType {
7
  private mockWidgetId = 0;
33✔
8
  private latestResponse: string | null = null;
33✔
9
  private executionMap = new Map<string, { resolve(value: string): void; reject(reason: unknown): void }>();
33✔
10

11
  public execute = jasmine
33✔
12
    .createSpy("grecaptcha.execute")
13
    .and.callFake((...args: Parameters<ReCaptchaV2.ReCaptcha["execute"]>) => {
14
      if (args.length > 1) {
10✔
15
        return this.grecaptchaExecute(args[0], args[1]);
8✔
16
      }
17

18
      return undefined;
2✔
19
    });
20
  public getResponse = jasmine.createSpy("grecaptcha.getResponse").and.callFake(() => this.latestResponse);
33✔
21
  public ready = jasmine.createSpy("grecaptcha.ready");
33✔
22
  public render = jasmine.createSpy("grecaptcha.render").and.callFake(() => ++this.mockWidgetId);
33✔
23
  public reset = jasmine.createSpy("grecaptcha.reset").and.callFake(() => (this.latestResponse = null));
33✔
24

25
  public get enterprise(): GrecaptchaMockedMethods {
26
    return this;
×
27
  }
28

29
  public emitGrecaptchaResponse(response: string): void {
30
    this.latestResponse = response;
8✔
31
    this.mostRecentRenderParams[1].callback(response);
8✔
32
  }
33

34
  public expireGrecaptchaResponse(): void {
35
    this.latestResponse = null;
1✔
36
    this.mostRecentRenderParams[1]["expired-callback"]();
1✔
37
  }
38

39
  public emitGrecaptchaError(): void {
40
    this.mostRecentRenderParams[1]["error-callback"]();
1✔
41
  }
42

43
  public expectNoErrorCallback(): void {
44
    expect(this.mostRecentRenderParams[1]["error-callback"]).toBeUndefined();
1✔
45
  }
46

47
  public executionFulfil(action: string, value: string): void {
48
    this.executionMap.get(action).resolve(value);
5✔
49
    this.executionMap.delete(action);
5✔
50
  }
51

52
  public executionReject(action: string, reason: unknown): void {
53
    this.executionMap.get(action).reject(reason);
2✔
54
    this.executionMap.delete(action);
2✔
55
  }
56

57
  private grecaptchaExecute(_siteKey: string, { action }: ReCaptchaV2.Action): PromiseLike<string> {
58
    let resolve: (value: string) => void;
59
    let reject: () => void;
60
    const promise = new Promise<string>((res, rej) => {
8✔
61
      resolve = res;
8✔
62
      reject = rej;
8✔
63
    });
64

65
    this.executionMap.set(action, { resolve, reject });
8✔
66

67
    return promise;
8✔
68
  }
69

70
  private get mostRecentRenderParams(): Parameters<ReCaptchaV2.ReCaptcha["render"]> {
71
    expect(this.render).toHaveBeenCalledTimes(1);
11✔
72
    const callArgs = this.render.calls.mostRecent().args as Parameters<ReCaptchaV2.ReCaptcha["render"]>;
11✔
73
    expect(callArgs.length).toEqual(2);
11✔
74

75
    return callArgs;
11✔
76
  }
77
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc