-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(helpers): add tests for
get
with no/falsy receiver
- Loading branch information
Showing
6 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
packages/babel-helpers/test/fixtures/behavior/get-with-falsy-receiver/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// ensure we test the helper implementation, | ||
// not built-in Reflect.get to which it defers | ||
delete Reflect; | ||
|
||
class Target { | ||
get typeOf() { | ||
return this === null ? "null" : typeof this; | ||
} | ||
}; | ||
|
||
// check that the 1st argument (target) *is not* used | ||
// in place of present but undefined 3rd argument (receiver) | ||
expect(HELPER_GET(new Target, "typeOf", undefined)).toBe("undefined"); | ||
|
||
// because the helper replaces itself upon invocation, | ||
// check it again with nullish arguments | ||
expect(HELPER_GET(new Target, "typeOf", undefined)).toBe("undefined"); | ||
expect(HELPER_GET(new Target, "typeOf", null)).toBe("null"); | ||
|
||
// check other falsy types | ||
expect(HELPER_GET(new Target, "typeOf", false)).toBe("boolean"); | ||
expect(HELPER_GET(new Target, "typeOf", 0)).toBe("number"); | ||
expect(HELPER_GET(new Target, "typeOf", "")).toBe("string"); |
3 changes: 3 additions & 0 deletions
3
packages/babel-helpers/test/fixtures/behavior/get-with-falsy-receiver/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"plugins": ["./plugin"] | ||
} |
12 changes: 12 additions & 0 deletions
12
packages/babel-helpers/test/fixtures/behavior/get-with-falsy-receiver/plugin.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module.exports = function() { | ||
return { | ||
visitor: { | ||
Identifier(path) { | ||
if (path.node.name === "HELPER_GET") { | ||
const helper = this.addHelper("get"); | ||
path.replaceWith(helper); | ||
} | ||
}, | ||
}, | ||
}; | ||
}; |
17 changes: 17 additions & 0 deletions
17
packages/babel-helpers/test/fixtures/behavior/get-without-receiver/exec.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// ensure we test the helper implementation, | ||
// not built-in Reflect.get to which it defers | ||
delete Reflect; | ||
|
||
class Target { | ||
get receiver() { | ||
return this; | ||
} | ||
}; | ||
|
||
// check that the 1st argument (target) *is* used | ||
// in place of missing 3rd argument (receiver) | ||
expect(HELPER_GET(new Target, "receiver")).toBeInstanceOf(Target); | ||
|
||
// because the helper replaces itself upon invocation, | ||
// check it again with the same arguments | ||
expect(HELPER_GET(new Target, "receiver")).toBeInstanceOf(Target); |
3 changes: 3 additions & 0 deletions
3
packages/babel-helpers/test/fixtures/behavior/get-without-receiver/options.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"plugins": ["./plugin"] | ||
} |
12 changes: 12 additions & 0 deletions
12
packages/babel-helpers/test/fixtures/behavior/get-without-receiver/plugin.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module.exports = function() { | ||
return { | ||
visitor: { | ||
Identifier(path) { | ||
if (path.node.name === "HELPER_GET") { | ||
const helper = this.addHelper("get"); | ||
path.replaceWith(helper); | ||
} | ||
}, | ||
}, | ||
}; | ||
}; |