Skip to content

Commit

Permalink
Merge pull request #237 from azu/refactor-notification
Browse files Browse the repository at this point in the history
Notification APIのサンプルコードを修正

close #236
  • Loading branch information
azu committed Jun 21, 2015
2 parents e4dbac4 + 43db567 commit 4d8224c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
10 changes: 6 additions & 4 deletions Ch4_AdvancedPromises/src/notifications/notification-callback.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
"use strict";
function notifyMessage(message, options, callback) {
if (Notification && Notification.permission === "granted") {
if (typeof Notification === "undefined") {
callback(new Error("doesn't support Notification API"));
return;
}
if (Notification.permission === "granted") {
var notification = new Notification(message, options);
callback(null, notification);
} else if (Notification.requestPermission) {
} else {
Notification.requestPermission(function (status) {
if (Notification.permission !== status) {
Notification.permission = status;
Expand All @@ -15,8 +19,6 @@ function notifyMessage(message, options, callback) {
callback(new Error("user denied"));
}
});
} else {
callback(new Error("doesn't support Notification API"));
}
}

Expand Down
16 changes: 8 additions & 8 deletions Ch4_AdvancedPromises/test/notification-thenable-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ describe("notification-thenable", function () {
delete global.Notification;
});
context("when already allowed permission", function () {
before(function () {
beforeEach(function () {
MockNotification.permission = "granted";
});
after(function () {
afterEach(function () {
delete MockNotification.permission;
});
it("should return Notification", function () {
Expand All @@ -28,8 +28,8 @@ describe("notification-thenable", function () {
});
});
context("when doesn't support Notification", function () {
before(function () {
global.Notification = null;
beforeEach(function () {
global.Notification = undefined;
});
it("should catch error", function () {
var promise = Promise.resolve(notifyMessageAsThenable("message"));
Expand All @@ -40,12 +40,12 @@ describe("notification-thenable", function () {
});
});
context("when user allow permission", function () {
before(function () {
beforeEach(function () {
MockNotification.requestPermission = function (callback) {
callback("granted");
};
});
after(function () {
afterEach(function () {
delete MockNotification.permission;
delete MockNotification.requestPermission;
});
Expand All @@ -58,12 +58,12 @@ describe("notification-thenable", function () {
});

context("when user deny permission", function () {
before(function () {
beforeEach(function () {
MockNotification.requestPermission = function (callback) {
callback("denied");
};
});
after(function () {
afterEach(function () {
delete MockNotification.permission;
delete MockNotification.requestPermission;
});
Expand Down

0 comments on commit 4d8224c

Please sign in to comment.