Skip to content

Commit

Permalink
add chrome.cookies.remove
Browse files Browse the repository at this point in the history
  • Loading branch information
acvetkov committed Jan 1, 2016
1 parent ded326e commit c16e31a
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 3 deletions.
15 changes: 14 additions & 1 deletion src/plugins/cookies/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export function assertGetAll(details, callback) {
}

/**
* assert chrome.cookies.set
* assert chrome.cookies.set arguments
* @param {AllCookieCriteria} details
*/
export function assertSet(details) {
Expand All @@ -49,6 +49,19 @@ export function assertSet(details) {
}
}

/**
* assert chrome.cookies.remove arguments
* @param {Object} details
*/
export function assertRemove(details) {
if (!_.isString(details.url)) {
throwError('url');
}
if (!_.isString(details.name)) {
throwError('name');
}
}

/**
* throws type error
* @param {String} argument
Expand Down
22 changes: 20 additions & 2 deletions src/plugins/cookies/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import _ from 'lodash';
import URI from 'URIjs';
import ChromeCookie from './cookie';
import ChromeEvent from '../../events';
import { assertGet, assertGetAll, assertSet } from './assert';
import { assertGet, assertGetAll, assertSet, assertRemove } from './assert';

export default class ChromeCookies {

Expand Down Expand Up @@ -74,8 +74,26 @@ export default class ChromeCookies {
this._invokeResult(cookieInfo, callback);
}

/**
* remove cookie
* @param {Object} details
* @param {String} details.url
* @param {String} details.name
* @param {Function} [callback]
*/
remove (details, callback) {

assertRemove.apply(null, arguments);
const params = {
name: details.name,
domain: (new URI(details.url)).hostname()
};
const cookieInfo = _.findWhere(this._state, params);
if (cookieInfo) {
const index = _.findIndex(this._state, cookieInfo);
this._state.splice(index, 1);
this._triggerChange({cause: 'explicit', removed: true, cookie: cookieInfo});
}
this._invokeResult(details, callback);
}

/**
Expand Down
68 changes: 68 additions & 0 deletions test/plugins/cookie/cookies.remove.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import sinon from 'sinon';
import _ from 'lodash';

import CookiesPlugin from '../../../src/plugins/cookies';
import state from '../data/cookie-state.json';
import { create } from '../../../src/chrome-api';

describe('plugins/cookies/remove', function () {

const chrome = create();

before(function () {
chrome.install(new CookiesPlugin());
});

beforeEach(function () {
chrome.cookies.onChanged.removeListeners();
chrome.cookies.state = _.cloneDeep(state);
});

it('should remove cookie', function (done) {
const params = {
name: 'MEGA_COOKIE_NAME',
url: 'http://www.kraken.ru'
};
const findParams = {
name: 'MEGA_COOKIE_NAME',
domain: 'www.kraken.ru'
};
assert.isObject(_.findWhere(chrome.cookies.state, findParams));
chrome.cookies.remove(params, function (details) {
assert.isUndefined(_.findWhere(chrome.cookies.state, findParams));
assert.equal(details.url, params.url);
assert.equal(details.name, params.name);
done();
});
});

it('should trigger change event', function (done) {
var spy = sinon.spy();
const params = {
name: 'MEGA_COOKIE_NAME',
url: 'http://www.kraken.ru'
};
chrome.cookies.onChanged.addListener(spy);
chrome.cookies.remove(params, function () {
assert.calledOnce(spy.withArgs({
cookie: state[0],
removed: true,
cause: 'explicit'
}));
done();
});
});

it('should not trigger change event', function (done) {
var spy = sinon.spy();
const params = {
name: 'MEGA_COOKIE_NAME_123',
url: 'http://www.kraken.ru'
};
chrome.cookies.onChanged.addListener(spy);
chrome.cookies.remove(params, function () {
assert.notCalled(spy);
done();
});
});
});

0 comments on commit c16e31a

Please sign in to comment.