Skip to content

Commit

Permalink
Fix: make AbortSignal and AbortController stringify correctly (#5)
Browse files Browse the repository at this point in the history
* Chore: increase test coverage
* Make AbortSignal and AbortController stringify correctly
  • Loading branch information
TimothyGu authored and mysticatea committed Mar 29, 2018
1 parent 9fdbd08 commit ec3a81f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/abort-controller.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,11 @@ Object.defineProperties(AbortController.prototype, {
abort: { enumerable: true },
})

if (typeof Symbol === "function" && typeof Symbol.toStringTag === "symbol") { //eslint-disable-line node/no-unsupported-features
Object.defineProperty(AbortController.prototype, Symbol.toStringTag, { //eslint-disable-line node/no-unsupported-features
configurable: true,
value: "AbortController",
})
}

export { AbortController, AbortSignal }
7 changes: 7 additions & 0 deletions src/abort-signal.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ Object.defineProperties(AbortSignal.prototype, {
},
})

if (typeof Symbol === "function" && typeof Symbol.toStringTag === "symbol") { //eslint-disable-line node/no-unsupported-features
Object.defineProperty(AbortSignal.prototype, Symbol.toStringTag, { //eslint-disable-line node/no-unsupported-features
configurable: true,
value: "AbortSignal",
})
}

defineEventAttribute(AbortSignal.prototype, "abort")

/**
Expand Down
32 changes: 32 additions & 0 deletions test/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { AbortController, AbortSignal } from "../src/abort-controller.mjs"

/*globals EventTarget */
const HAS_EVENT_TARGET_INTERFACE = (typeof EventTarget !== "undefined")
const SUPPORTS_TOSTRINGTAG = (typeof Symbol === "function" && typeof Symbol.toStringTag === "symbol") //eslint-disable-line node/no-unsupported-features

/**
* Assert a condition.
Expand Down Expand Up @@ -38,6 +39,17 @@ describe("AbortController", () => {
controller = new AbortController()
})

it("should not be callable", () => {
try {
AbortController()
}
catch (e) {
assert(e instanceof TypeError)
return
}
throw new Error("should throw a TypeError")
})

it("should have 2 properties", () => {
// IE does not support Set constructor.
const keys = new Set()
Expand All @@ -54,6 +66,10 @@ describe("AbortController", () => {
})
})

;(SUPPORTS_TOSTRINGTAG ? it : xit)("should be stringified as [object AbortController]", () => {
assert(controller.toString() === "[object AbortController]")
})

describe("'signal' property", () => {
let signal = null

Expand Down Expand Up @@ -159,6 +175,17 @@ describe("AbortController", () => {
})

describe("AbortSignal", () => {
it("should not be callable", () => {
try {
AbortSignal()
}
catch (e) {
assert(e instanceof TypeError)
return
}
throw new Error("should throw a TypeError")
})

it("should throw a TypeError when it's constructed directly", () => {
try {
new AbortSignal() //eslint-disable-line no-new
Expand All @@ -169,4 +196,9 @@ describe("AbortSignal", () => {
}
throw new Error("should throw a TypeError")
})

;(SUPPORTS_TOSTRINGTAG ? it : xit)("should be stringified as [object AbortSignal]", () => {
const signal = new AbortController().signal
assert(signal.toString() === "[object AbortSignal]")
})
})

0 comments on commit ec3a81f

Please sign in to comment.