Add developers to send arbitrary data to the config page

This commit is contained in:
Keegan
2016-03-11 03:36:00 +11:00
parent b1a6f184ba
commit e5496c62d6
6 changed files with 77 additions and 51 deletions
+14 -4
View File
@@ -11,12 +11,13 @@ var componentRegistry = require('../src/scripts/lib/component-registry');
var idCounter = 0;
/**
* @param {Object} [extra] - add/replace keys in the meta
* @returns {{accountToken: string, watchToken: string, activeWatchInfo: {platform:
* string, model: string, language: string, firmware: {major: number, minor:
* number, patch: number, suffix: string}}}}
* number, patch: number, suffix: string}}, userData: {}}}
*/
module.exports.meta = function() {
return {
module.exports.meta = function(extra) {
var result = {
accountToken: '0123456789abcdef0123456789abcdef',
watchToken: '0123456789abcdef0123456789abcdef',
activeWatchInfo: {
@@ -29,8 +30,15 @@ module.exports.meta = function() {
patch: 2,
suffix: ''
}
}
},
userData: {}
};
_.eachObj(extra || {}, function(key, val) {
result[key] = val;
});
return result;
};
/**
@@ -106,6 +114,8 @@ module.exports.clayConfig = function(types, build, autoRegister, settings) {
* @param {Object} [options] - Additional options to pass to Clay
* @param {boolean} [options.autoHandleEvents] - If false, Clay will not
* automatically handle the 'showConfiguration' and 'webviewclosed' events
* @param {*} [options.userData={}] - Arbitrary data to pass to the config page. Will
* be available os `clayConfig.meta.userData`
* @param {boolean} [destroyLocalStorage=true]
* @return {Clay}
*/
+16 -31
View File
@@ -7,31 +7,19 @@ var standardComponents = require('../../src/scripts/components');
var sinon = require('sinon');
var toSource = require('tosource');
var accountToken = '0123456789abcdef0123456789abcdef';
var watchToken = '0123456789abcdef0123456789abcdef';
var activeWatchInfo = {
platform: 'chalk',
model: 'qemu_platform_chalk',
language: 'en_US',
firmware: {
major: 3,
minor: 3,
patch: 2,
suffix: ''
}
};
/**
* @return {void}
*/
function stubPebble() {
var meta = fixture.meta();
global.Pebble = {
addEventListener: sinon.stub(),
openURL: sinon.stub(),
sendAppMessage: sinon.stub(),
getActiveWatchInfo: sinon.stub().returns(activeWatchInfo),
getAccountToken: sinon.stub().returns(accountToken),
getWatchToken: sinon.stub().returns(watchToken)
getActiveWatchInfo: sinon.stub().returns(meta.activeWatchInfo),
getAccountToken: sinon.stub().returns(meta.accountToken),
getWatchToken: sinon.stub().returns(meta.watchToken)
};
}
@@ -310,37 +298,34 @@ describe('Clay', function() {
var emptyMeta = {
activeWatchInfo: null,
accountToken: '',
watchToken: ''
watchToken: '',
userData: {}
};
it('populates the meta in the showConfiguration handler', function() {
stubPebble();
var clay = fixture.clay([]);
var userData = {foo: 'bar'};
var clay = fixture.clay([], null, {userData: userData});
// meta only gets populated after showConfiguration happens
assert.deepEqual(clay.meta, emptyMeta);
Pebble.addEventListener.withArgs('showConfiguration').callArg(1);
assert.deepEqual(clay.meta, {
activeWatchInfo: activeWatchInfo,
accountToken: accountToken,
watchToken: watchToken
});
assert.deepEqual(clay.meta, fixture.meta({userData: userData}));
});
it('populates the meta in the ready handler', function() {
stubPebble();
var clay = fixture.clay([], null, {autoHandleEvents: false});
var userData = {foo: 'bar'};
var clay = fixture.clay([], null, {
autoHandleEvents: false,
userData: userData
});
// meta only gets populated after ready happens
assert.deepEqual(clay.meta, emptyMeta);
Pebble.addEventListener.withArgs('ready').callArg(1);
assert.deepEqual(clay.meta, {
activeWatchInfo: activeWatchInfo,
accountToken: accountToken,
watchToken: watchToken
});
assert.deepEqual(clay.meta, fixture.meta({userData: userData}));
});
it('populates the meta with with empty values when there is no Pebble global',