From 2a3191d028bf838d85e09fa439a47e00472e3c7b Mon Sep 17 00:00:00 2001 From: Robert Webb Date: Sun, 21 Jun 2020 22:51:11 -0700 Subject: [PATCH] [jasmine-3.1] Add the`saveArgumentsByValue` function (#45049) See https://github.com/jasmine/jasmine/blob/15f969bee7066526ba44e562407c728d4a1740df/src/core/CallTracker.js\#L117 for definition Co-authored-by: Robert Webb --- types/jasmine/index.d.ts | 2 ++ types/jasmine/jasmine-tests.ts | 12 ++++++++++++ types/jasmine/ts3.1/index.d.ts | 2 ++ types/jasmine/ts3.1/jasmine-tests.ts | 12 ++++++++++++ types/jasmine/v2/index.d.ts | 2 ++ types/jasmine/v2/jasmine-tests.ts | 12 ++++++++++++ 6 files changed, 42 insertions(+) diff --git a/types/jasmine/index.d.ts b/types/jasmine/index.d.ts index 01e734068f..02185cdbe4 100644 --- a/types/jasmine/index.d.ts +++ b/types/jasmine/index.d.ts @@ -914,6 +914,8 @@ declare namespace jasmine { first(): CallInfo; /** By chaining the spy with calls.reset(), will clears all tracking for a spy */ reset(): void; + /** Set this spy to do a shallow clone of arguments passed to each invocation. */ + saveArgumentsByValue(): void; } interface CallInfo { diff --git a/types/jasmine/jasmine-tests.ts b/types/jasmine/jasmine-tests.ts index 100bed9fee..280f3658cd 100644 --- a/types/jasmine/jasmine-tests.ts +++ b/types/jasmine/jasmine-tests.ts @@ -794,6 +794,18 @@ describe("A spy", () => { expect(foo.setBar.calls.any()).toBe(false); }); + + it("can save arguments by value.", () => { + const arr = [1]; + foo.setBar.calls.saveArgumentsByValue(); + + foo.setBar(arr); + arr.push(2); + foo.setBar(arr); + + expect(foo.setBar.calls.argsFor(0)[0]).toEqual([1]); + expect(foo.setBar.calls.argsFor(1)[0]).toEqual([1, 2]); + }); }); describe("A spy, when created manually", () => { diff --git a/types/jasmine/ts3.1/index.d.ts b/types/jasmine/ts3.1/index.d.ts index d27f386d04..2f135cc699 100644 --- a/types/jasmine/ts3.1/index.d.ts +++ b/types/jasmine/ts3.1/index.d.ts @@ -953,6 +953,8 @@ declare namespace jasmine { first(): CallInfo; /** By chaining the spy with calls.reset(), will clears all tracking for a spy */ reset(): void; + /** Set this spy to do a shallow clone of arguments passed to each invocation. */ + saveArgumentsByValue(): void; } interface CallInfo { diff --git a/types/jasmine/ts3.1/jasmine-tests.ts b/types/jasmine/ts3.1/jasmine-tests.ts index 96b5fc48c5..f8eabd65b5 100644 --- a/types/jasmine/ts3.1/jasmine-tests.ts +++ b/types/jasmine/ts3.1/jasmine-tests.ts @@ -794,6 +794,18 @@ describe("A spy", () => { expect(foo.setBar.calls.any()).toBe(false); }); + + it("can save arguments by value.", () => { + const arr = [1]; + foo.setBar.calls.saveArgumentsByValue(); + + foo.setBar(arr); + arr.push(2); + foo.setBar(arr); + + expect(foo.setBar.calls.argsFor(0)[0]).toEqual([1]); + expect(foo.setBar.calls.argsFor(1)[0]).toEqual([1, 2]); + }); }); describe("A spy, when created manually", () => { diff --git a/types/jasmine/v2/index.d.ts b/types/jasmine/v2/index.d.ts index e22aca8618..5c43ae4d4d 100644 --- a/types/jasmine/v2/index.d.ts +++ b/types/jasmine/v2/index.d.ts @@ -669,6 +669,8 @@ declare namespace jasmine { first(): CallInfo; /** By chaining the spy with calls.reset(), will clears all tracking for a spy **/ reset(): void; + /** Set this spy to do a shallow clone of arguments passed to each invocation. */ + saveArgumentsByValue(): void; } interface CallInfo { diff --git a/types/jasmine/v2/jasmine-tests.ts b/types/jasmine/v2/jasmine-tests.ts index 9dc67c8e8d..199a24f2f9 100644 --- a/types/jasmine/v2/jasmine-tests.ts +++ b/types/jasmine/v2/jasmine-tests.ts @@ -610,6 +610,18 @@ describe("A spy", () => { expect(foo.setBar.calls.any()).toBe(false); }); + + it("can save arguments by value.", () => { + const arr = [1]; + foo.setBar.calls.saveArgumentsByValue(); + + foo.setBar(arr); + arr.push(2); + foo.setBar(arr); + + expect(foo.setBar.calls.argsFor(0)[0]).toEqual([1]); + expect(foo.setBar.calls.argsFor(1)[0]).toEqual([1, 2]); + }); }); describe("A spy, when created manually", () => {