Compare commits

..
10 Commits
Author SHA1 Message Date
Keegan 2b7ad09eb4 Release v1.0.4 2016-11-21 11:34:06 -08:00
Keegan Lillo 4b53d920c7 #77 - Add docs for setSettings() (#155) 2016-11-21 11:32:53 -08:00
Keegan Lillo fa3406a809 #151 - Fix incorrect value being set when defaultValue is falsey (#152) 2016-10-21 12:11:12 -07:00
Yohan Robert 46c0adb62c #77 - Allow claySettings to be modified from app.js (#150)
* .setSettings(key, value) to set a single setting
* .setSettings(object) to set multiple settings at once
2016-10-21 10:36:55 -07:00
Keegan Lillo d18170337d Release/v1.0.3 (#148) 2016-10-18 09:47:32 -07:00
Keegan Lillo 9c7238d578 #124/get items by group (#147)
* Add getItemsByGroup function

* Update test

* update readme

* Update README for submit button

* Update docs with ID prop for button and submit components
2016-10-18 09:40:32 -07:00
Keegan Lillo 1a63761e43 #141 - Use index.js not app.js in docs (#142) 2016-09-29 01:15:45 -07:00
Ben Combee 9c6bb48c7a fix copy-paste error with messageKey in ClayItem description (#138) 2016-09-14 23:42:36 -07:00
Jon Barlow 2173ad7c71 Add some note about Rocky.js (#134) 2016-08-17 13:51:22 +01:00
Keegan Lillo b8db6c6a90 Direct pebble.js users to v0.1.7 and fix typos 2016-07-11 11:29:06 -07:00
7 changed files with 152 additions and 62 deletions
+33 -56
View File
@@ -12,7 +12,7 @@ If you would like to contribute to Clay, check out the [contributing guide.](CON
1. Run `pebble package install pebble-clay` to install the package in your project
2. Create a JSON file called `config.json` and place it in your `src/js` directory.
3. In order for JSON files to work you may need to change a line in your `wscript` from `ctx.pbl_bundle(binaries=binaries, js=ctx.path.ant_glob('src/js/**/*.js'))` to `ctx.pbl_bundle(binaries=binaries, js=ctx.path.ant_glob(['src/js/**/*.js', 'src/js/**/*.json']))`.
4. Your `app.js` file needs to `require` clay and your config file, then be initialized. Clay will by default automatically handle the 'showConfiguration' and 'webviewclosed' events. Copy and paste the following into the top of your `app.js` file:
4. Your `index.js` (`app.js` in SDK 3) file needs to `require` clay and your config file, then be initialized. Clay will by default automatically handle the 'showConfiguration' and 'webviewclosed' events. Copy and paste the following into the top of your `index.js` file:
```javascript
var Clay = require('pebble-clay');
@@ -32,7 +32,7 @@ If you would like to contribute to Clay, check out the [contributing guide.](CON
```javascript
module.exports = [];
```
5. Your `app.js` file needs to `require` clay and your config file, then be initialized. Clay will by default automatically handle the 'showConfiguration' and 'webviewclosed' events. Copy and paste the following into the top of your `app.js` file:
5. Your `index.js` file needs to `require` clay and your config file, then be initialized. Clay will by default automatically handle the 'showConfiguration' and 'webviewclosed' events. Copy and paste the following into the top of your `app.js` file:
```javascript
var Clay = require('pebble-clay');
@@ -42,6 +42,12 @@ If you would like to contribute to Clay, check out the [contributing guide.](CON
6. Next is the fun part - creating your config page. Edit your `config.js` file to build a layout of elements as described in the sections below.
7. Make sure you have defined all of your message keys using `Automatic assignment` in your project settings. More info on how that works [here.](https://developer.pebble.com/guides/communication/using-pebblekit-js/#defining-keys)
# Getting Started (Pebble.js)
If you are using [Pebble.js](https://developer.pebble.com/docs/pebblejs/) and would like to use Clay, The setup process is a little different. Pebble.js does not currently support message keys so you will have to use [v0.1.7](https://github.com/pebble/clay/releases/v0.1.7) of Clay. Follow the instructions in the [readme for that version.](https://github.com/pebble/clay/blob/v0.1.7/README.md)
# Getting Started (Rocky.js)
If you are using [Rocky.js](https://developer.pebble.com/docs/rockyjs/) and would like to use Clay, please be aware that this is currently unsupported. It is possible to install the Clay package and override the 'showConfiguration' and 'webviewclosed' events and handle them manually, but Rocky.js does not currently support persistent storage, so the settings must be loaded from the phone each time. You can find an example of using Clay with Rocky.js [here](https://github.com/orviwan/rocky-leco-clay).
# Creating Your Config File
Clay uses JavaScript object notation (or JSON) to generate the config page for you. The structure of the page is up to you, but you do need to follow some basic rules.
@@ -120,11 +126,12 @@ Headings can be used in anywhere and can have their size adjusted to suit the co
| Property | Type | Description |
|----------|------|-------------|
| type | string | Set to `heading`. |
| id | string (unique) | Set this to a unique string to allow this item to be looked up using `Clay.getItemsById()` in your [custom function](#custom-function). |
| id | string (unique) | Set this to a unique string to allow this item to be looked up using `Clay.getItemById()` in your [custom function](#custom-function). |
| messageKey | string (unique) | The AppMessage key matching the `messageKey` item defined in your `package.json`. Set this to a unique string to allow this item to be looked up using `Clay.getItemsByMessageKey()` in your custom function. You must set this if you wish for the value of this item to be persisted after the user closes the config page. |
| defaultValue | string/HTML | The heading's text. |
| size | int | Defaults to `4`. An integer from 1 to 6 where 1 is the largest size and 6 is the smallest. (represents HTML `<h1>`, `<h2>`, `<h3>`, etc). |
| capabilities | array | Array of features that the connected watch must have for this item to be present |
| group | string | Set this to allow this item, along with other items sharing the same group to be looked up using `Clay.getItemsByGroup()` in your [custom function](#custom-function) |
##### Example
@@ -151,10 +158,11 @@ Text is used to provide descriptions of sections or to explain complex parts of
| Property | Type | Description |
|----------|------|-------------|
| type | string | Set to `text`. |
| id | string (unique) | Set this to a unique string to allow this item to be looked up using `Clay.getItemsById()` in your [custom function](#custom-function). |
| id | string (unique) | Set this to a unique string to allow this item to be looked up using `Clay.getItemById()` in your [custom function](#custom-function). |
| messageKey | string (unique) | The AppMessage key matching the `messageKey` item defined in your `package.json`. Set this to a unique string to allow this item to be looked up using `Clay.getItemsByMessageKey()` in your custom function. You must set this if you wish for the value of this item to be persisted after the user closes the config page. |
| defaultValue | string/HTML | The content of the text element. |
| capabilities | array | Array of features that the connected watch must have for this item to be present |
| group | string | Set this to allow this item, along with other items sharing the same group to be looked up using `Clay.getItemsByGroup()` in your [custom function](#custom-function) |
##### Example
@@ -179,13 +187,14 @@ Standard text input field.
| Property | Type | Description |
|----------|------|-------------|
| type | string | Set to `input`. |
| id | string (unique) | Set this to a unique string to allow this item to be looked up using `Clay.getItemsById()` in your [custom function](#custom-function). |
| id | string (unique) | Set this to a unique string to allow this item to be looked up using `Clay.getItemById()` in your [custom function](#custom-function). |
| messageKey | string (unique) | The AppMessage key matching the `messageKey` item defined in your `package.json`. Set this to a unique string to allow this item to be looked up using `Clay.getItemsByMessageKey()` in your custom function. You must set this if you wish for the value of this item to be persisted after the user closes the config page. |
| label | string | The label that should appear next to this item. |
| defaultValue | string | The default value of the input field. |
| description | string | Optional sub-text to include below the component |
| attributes | object | An object containing HTML attributes to set on the input field. Set `type` to values such as "email", "time", "date" etc to adjust the behavior of the component. |
| capabilities | array | Array of features that the connected watch must have for this item to be present |
| group | string | Set this to allow this item, along with other items sharing the same group to be looked up using `Clay.getItemsByGroup()` in your [custom function](#custom-function) |
##### Example
@@ -217,12 +226,13 @@ Switch for a single item.
| Property | Type | Description |
|----------|------|-------------|
| type | string | Set to `toggle`. |
| id | string (unique) | Set this to a unique string to allow this item to be looked up using `Clay.getItemsById()` in your [custom function](#custom-function). |
| id | string (unique) | Set this to a unique string to allow this item to be looked up using `Clay.getItemById()` in your [custom function](#custom-function). |
| messageKey | string (unique) | The AppMessage key matching the `messageKey` item defined in your `package.json`. Set this to a unique string to allow this item to be looked up using `Clay.getItemsByMessageKey()` in your custom function. You must set this if you wish for the value of this item to be persisted after the user closes the config page. |
| label | string | The label that should appear next to this item. |
| defaultValue | int\|boolean | The default value of the toggle. Defaults to `false` if not specified. |
| description | string | Optional sub-text to include below the component |
| capabilities | array | Array of features that the connected watch must have for this item to be present |
| group | string | Set this to allow this item, along with other items sharing the same group to be looked up using `Clay.getItemsByGroup()` in your [custom function](#custom-function) |
##### Example
@@ -249,13 +259,14 @@ A dropdown menu containing multiple options.
| Property | Type | Description |
|----------|------|-------------|
| type | string | Set to `select`. |
| id | string (unique) | Set this to a unique string to allow this item to be looked up using `Clay.getItemsById()` in your [custom function](#custom-function). |
| id | string (unique) | Set this to a unique string to allow this item to be looked up using `Clay.getItemById()` in your [custom function](#custom-function). |
| messageKey | string (unique) | The AppMessage key matching the `messageKey` item defined in your `package.json`. Set this to a unique string to allow this item to be looked up using `Clay.getItemsByMessageKey()` in your custom function. You must set this if you wish for the value of this item to be persisted after the user closes the config page. |
| label | string | The label that should appear next to this item. |
| defaultValue | string | The default value of the dropdown menu. Must match a value in the `options` array. |
| description | string | Optional sub-text to include below the component |
| options | array of objects | The options you want to appear in the dropdown menu. Each option is an object with a `label` and `value` property. |
| capabilities | array | Array of features that the connected watch must have for this item to be present |
| group | string | Set this to allow this item, along with other items sharing the same group to be looked up using `Clay.getItemsByGroup()` in your [custom function](#custom-function) |
##### Example
@@ -355,7 +366,7 @@ The color picker will automatically show a different layout depending on the wat
| Property | Type | Description |
|----------|------|-------------|
| type | string | Set to `color`. |
| id | string (unique) | Set this to a unique string to allow this item to be looked up using `Clay.getItemsById()` in your [custom function](#custom-function). |
| id | string (unique) | Set this to a unique string to allow this item to be looked up using `Clay.getItemById()` in your [custom function](#custom-function). |
| messageKey | string (unique) | The AppMessage key matching the `messageKey` item defined in your `package.json`. Set this to a unique string to allow this item to be looked up using `Clay.getItemsByMessageKey()` in your custom function. You must set this if you wish for the value of this item to be persisted after the user closes the config page. |
| label | string | The label that should appear next to this item. |
| defaultValue | string OR int | The default color. One of the [64 colors](https://developer.pebble.com/guides/tools-and-resources/color-picker/) compatible with Pebble smartwatches. Always use the uncorrected value even if `sunlight` is true. The component will do the conversion internally. |
@@ -364,6 +375,7 @@ The color picker will automatically show a different layout depending on the wat
| layout | string OR array | Optional. Use a custom layout for the color picker. Defaults to automatically choosing the most appropriate layout for the connected watch. The layout is represented by a two dimensional array. Use `false` to insert blank spaces. You may also use one of the preset layouts by setting `layout` to: `"COLOR"`, `"GRAY"` or `"BLACK_WHITE"` |
| allowGray | boolean | Optional. Set this to `true` to include gray (`#AAAAAA`) in the color picker for aplite running on firmware 3 and above. This is optional because only a subset of the drawing operations support gray on aplite. Defaults to `false` |
| capabilities | array | Array of features that the connected watch must have for this item to be present |
| group | string | Set this to allow this item, along with other items sharing the same group to be looked up using `Clay.getItemsByGroup()` in your [custom function](#custom-function) |
##### Example
@@ -421,13 +433,14 @@ A list of options allowing the user can only choose one option to submit.
| Property | Type | Description |
|----------|------|-------------|
| type | string | Set to `radiogroup`. |
| id | string (unique) | Set this to a unique string to allow this item to be looked up using `Clay.getItemsById()` in your [custom function](#custom-function). |
| id | string (unique) | Set this to a unique string to allow this item to be looked up using `Clay.getItemById()` in your [custom function](#custom-function). |
| messageKey | string (unique) | The AppMessage key matching the `messageKey` item defined in your `package.json`. Set this to a unique string to allow this item to be looked up using `Clay.getItemsByMessageKey()` in your custom function. You must set this if you wish for the value of this item to be persisted after the user closes the config page. |
| label | string | The label that should appear next to this item. |
| defaultValue | string | The default selected item. Must match a value in the `options` array. |
| description | string | Optional sub-text to include below the component |
| options | array of objects | The options you want to appear in the radio group. Each option is an object with a `label` and `value` property. |
| capabilities | array | Array of features that the connected watch must have for this item to be present |
| group | string | Set this to allow this item, along with other items sharing the same group to be looked up using `Clay.getItemsByGroup()` in your [custom function](#custom-function) |
##### Example
@@ -466,13 +479,14 @@ A list of options where a user may choose more than one option to submit.
| Property | Type | Description |
|----------|------|-------------|
| type | string | Set to `checkboxgroup`. |
| id | string (unique) | Set this to a unique string to allow this item to be looked up using `Clay.getItemsById()` in your [custom function](#custom-function). |
| id | string (unique) | Set this to a unique string to allow this item to be looked up using `Clay.getItemById()` in your [custom function](#custom-function). |
| messageKey | string (unique) | The AppMessage key matching the `messageKey` item defined in your `package.json`. Set this to a unique string to allow this item to be looked up using `Clay.getItemsByMessageKey()` in your custom function. You must set this if you wish for the value of this item to be persisted after the user closes the config page. **NOTE:** The checkboxgroup component will expect you to have defined a `messageKey` in your `package.json` using array syntax. In the example below, the matching entry in the `package.json` would be `favorite_food[3]`. In CloudPebble, you must use `Automatic assignment` for message keys and the `Key Array Length` must match the number of options in the checkboxgroup |
| label | string | The label that should appear next to this item. |
| defaultValue | array of booleans | The default selected items. |
| description | string | Optional sub-text to include below the component |
| options | array of strings | The labels for each checkbox you want to appear in the checkbox group. |
| capabilities | array | Array of features that the connected watch must have for this item to be present |
| group | string | Set this to allow this item, along with other items sharing the same group to be looked up using `Clay.getItemsByGroup()` in your [custom function](#custom-function) |
##### Example
@@ -499,10 +513,12 @@ In the above example, Sushi and Burgers will be selected by default.
| Property | Type | Description |
|----------|------|-------------|
| type | string | Set to `button`. |
| id | string (unique) | Set this to a unique string to allow this item to be looked up using `Clay.getItemById()` in your [custom function](#custom-function). |
| defaultValue | string | The text displayed on the button. |
| primary | boolean | If `true` the button will be orange, if `false`, the button will be gray (defaults to `false`)|
| description | string | Optional sub-text to include below the component |
| capabilities | array | Array of features that the connected watch must have for this item to be present |
| group | string | Set this to allow this item, along with other items sharing the same group to be looked up using `Clay.getItemsByGroup()` in your [custom function](#custom-function) |
##### Example
@@ -541,6 +557,7 @@ you must just remember to divide the received value on the watch accordingly.
| step | number | The multiple of the values allowed to be set on the slider. The slider will snap to these values. This value also determines the precision used when the value is sent to the watch. Defaults to 1 |
| description | string | Optional sub-text to include below the component |
| capabilities | array | Array of features that the connected watch must have for this item to be present |
| group | string | Set this to allow this item, along with other items sharing the same group to be looked up using `Clay.getItemsByGroup()` in your [custom function](#custom-function) |
##### Example
@@ -571,7 +588,9 @@ The submit button for the page. You **MUST** include this component somewhere in
|----------|------|-------------|
| type | string | Set to `submit`. |
| defaultValue | string | The text displayed on the button. |
| id | string (unique) | Set this to a unique string to allow this item to be looked up using `Clay.getItemById()` in your [custom function](#custom-function). |
| capabilities | array | Array of features that the connected watch must have for this item to be present |
| group | string | Set this to allow this item, along with other items sharing the same group to be looked up using `Clay.getItemsByGroup()` in your [custom function](#custom-function) |
##### Example
@@ -822,51 +841,6 @@ Pebble.addEventListener('webviewclosed', function(e) {
});
```
## Clay and Pebble.js
If you are using [Pebble.js](https://developer.pebble.com/docs/pebblejs/) and would like to use Clay, There are some extra steps to follow:
1. Remove all your `Settings.config` calls. Clay will handle the settings from now on.
2. Use the example below to modify your `app.js`. Notice that `autoHandleEvents` is set to `false` in the Clay constructor.:
```javascript
var Settings = require('settings');
var Clay = require('pebble-clay');
var clayConfig = require('./config');
var clay = new Clay(clayConfig, null, {autoHandleEvents: false});
Pebble.addEventListener('showConfiguration', function(e) {
Pebble.openURL(clay.generateUrl());
});
Pebble.addEventListener('webviewclosed', function(e) {
if (e && !e.response) {
return;
}
var dict = clay.getSettings(e.response);
// Save the Clay settings to the Settings module.
Settings.option(dict);
});
```
### Caveats
Clay treats colors as numbers. You will need to convert these numbers to CSS colors before they can be used by Pebble.js. Use the function below to convert the colors.
```javascript
function cssColor(color) {
color = color.toString(16);
while (color.length < 6) {
color = '0' + color;
}
return '#' + color;
}
// Example usage with default:
cssColor(Settings.option('BACKGROUND_COLOR') || 0xff0000);
```
## Clay API (app.js)
### `Clay([Array] config, [function] customFn, [object] options)`
@@ -902,6 +876,8 @@ cssColor(Settings.option('BACKGROUND_COLOR') || 0xff0000);
| `.registerComponent( [ClayComponent] component )` <br> Registers a custom component. | `void`. |
| `.generateUrl()` | `string` - The URL to open with `Pebble.openURL()` to use the Clay-generated config page. |
| `.getSettings( [object] response, [boolean] convert=true)` <br> `response` - the response object provided to the "webviewclosed" event <br> `convert` - Pass `false` to not convert the settings to be compatible with `Pebble.sendAppMessage()` | `Object` - object of keys and values for each config page item with an `messageKey`, where the key is the `messageKey` and the value is the chosen value of that item. <br><br>This method will do some conversions depending on the type of the setting. Arrays will use message key array syntax to make the values of each item in the array available as individual message keys. Booleans will be converted to numbers. eg `true` becomes `1` and `false` becomes `0`. If the value is a number or an array of numbers and the optional property: "precision" is the power of precision (value * 10 ^ precision) and then floored. Eg: `1.4567` with a precision set to `3` will become `1456`. Pass `false` as the second parameter to disable this behavior. See the example below for how this all works |
| `.setSettings( [string] key, [*] value )` <br> `key` - The key for the setting you want to set. <br> `value` - The value of the setting you want to set. | `void` |
| `.setSettings( [object] settings )` <br> `settings` - An object of key/value pairs that you would like to update the settings with. eg `{ user_name: 'Emily', show_animations: true }` | `void` |
#### `.getSettings()` Example
@@ -1030,6 +1006,7 @@ This is the main way of talking to your generated config page. An instance of th
| `.getItemByMessageKey( [string] messageKey )` | `ConfigItem\|undefined` - a single `ConfigItem` that has the provided `messageKey`, otherwise `undefined`. |
| `.getItemById( [string] id )` | `ConfigItem\|undefined` - a single `ConfigItem` that has the provided `id`, otherwise `undefined`. |
| `.getItemsByType( [string] type )` | `Array.<ConfigItem>` - an array of config items that match the provided `type`. |
| `.getItemsByGroup( [string] group )` | `Array.<ConfigItem>` - an array of config items that match the provided `group`. |
| `.serialize()` | `Object` - an object representing all items with an `messageKey` where the key is the `messageKey` and the value is an object with the `value` property set to the result of running `.get()` on the Clay item. If the Clay item has a `precision` property set, it is included in the object |
| `.build()` <br> Builds the config page. Will dispatch the `BEFORE_BUILD` event prior to building the page, then the `AFTER_BUILD` event once it is complete. If the config page has already been built, then the `ClayConfig.destroy()` method will be executed prior to building the page again. | `ClayConfig` |
| `.destroy()` <br> Destroys the config page. Will dispatch the `BEFORE_DESTROY` event prior to destroying the page, then the `AFTER_DESTROY` event once it is complete. This method wipes the config page completely, including all existing items. You will need to make sure that you re-attach your event handlers for any items that are replaced | `ClayConfig` |
@@ -1047,7 +1024,7 @@ This is the main way of talking to your generated config page. An instance of th
| Property | Type | Description |
|----------|------|-------------|
| `.id` | String | The ID of the item if provided in the config. |
| `.messageKey` | String | The ID of the item if provided in the config. |
| `.messageKey` | String | The messageKey of the item if provided in the config. |
| `.config` | Object | Reference to the config passed to the constructer. |
| `$element` | $Minified | A Minified list representing the root HTML element of the config item. |
| `$manipulatorTarget` | $Minified | A Minified list representing the HTML element with **data-manipulator-target** set. This is generally pointing to the main `<input>` element and will be used for binding events. |
+3 -3
View File
@@ -64,11 +64,11 @@ module.exports = [
{
"type": "slider",
"messageKey": "slider",
"defaultValue": 15,
"defaultValue": 0,
"label": "Slider",
"description": "This is the description for the slider",
"min": 10,
"max": 20,
"min": 0,
"max": 30,
"step": 0.25
},
{
+33
View File
@@ -213,6 +213,39 @@ Clay.prototype.getSettings = function(response, convert) {
return convert === false ? settings : Clay.prepareSettingsForAppMessage(settings);
};
/**
* Updates the settings with the given value(s).
*
* @signature `clay.setSettings(key, value)`
* @param {String} key - The property to set.
* @param {*} value - the value assigned to _key_.
* @return {undefined}
*
* @signature `clay.setSettings(settings)`
* @param {Object} settings - an object containing the key/value pairs to be set.
* @return {undefined}
*/
Clay.prototype.setSettings = function(key, value) {
var settingsStorage = {};
try {
settingsStorage = JSON.parse(localStorage.getItem('clay-settings')) || {};
} catch (e) {
console.error(e.toString());
}
if (typeof key === 'object') {
var settings = key;
Object.keys(settings).forEach(function(key) {
settingsStorage[key] = settings[key];
});
} else {
settingsStorage[key] = value;
}
localStorage.setItem('clay-settings', JSON.stringify(settingsStorage));
};
/**
* @param {string} input
* @param {string} [prefix='data:text/html;charset=utf-8,']
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "pebble-clay",
"version": "1.0.2",
"version": "1.0.4",
"description": "Pebble Config Framework",
"scripts": {
"test-travis": "./node_modules/.bin/gulp && ./node_modules/.bin/karma start ./test/karma.conf.js --single-run --browsers chromeTravisCI && ./node_modules/.bin/eslint ./",
+13 -2
View File
@@ -86,9 +86,9 @@ function ClayConfig(settings, config, $rootContainer, meta) {
// set the value of the item via the manipulator to ensure consistency
var value = typeof _settings[_item.messageKey] !== 'undefined' ?
_settings[_item.messageKey] :
(_item.defaultValue || '');
_item.defaultValue;
clayItem.set(value);
clayItem.set(typeof value !== 'undefined' ? value : '');
$container.add(clayItem.$element);
}
@@ -182,6 +182,17 @@ function ClayConfig(settings, config, $rootContainer, meta) {
});
};
/**
* @param {string} group
* @returns {Array.<ClayItem>}
*/
self.getItemsByGroup = function(group) {
_checkBuilt('getItemsByGroup');
return _items.filter(function(item) {
return item.config.group === group;
});
};
/**
* @returns {Object}
*/
+48
View File
@@ -386,6 +386,54 @@ describe('Clay', function() {
});
});
describe('.setSettings', function() {
it('it writes to localStorage when passing an object',
function() {
var clay = fixture.clay([]);
var settings = {
key1: 'value1',
key2: 'value2'
};
var expected = {
key1: 'value1',
key2: 'value2'
};
clay.setSettings(settings);
assert.equal(
localStorage.getItem('clay-settings'),
JSON.stringify(expected)
);
});
it('it writes to localStorage when passing a key and a value',
function() {
var clay = fixture.clay([]);
var expected = {
key1: 'value1',
key2: 'value2%7Dbreaks'
};
clay.setSettings('key1', 'value1');
clay.setSettings('key2', 'value2%7Dbreaks');
assert.equal(localStorage.getItem('clay-settings'),
JSON.stringify(expected)
);
});
it('doesn\'t throw and logs an error if settings in localStorage are broken',
function() {
var clay = fixture.clay([]);
var errorStub = sinon.stub(console, 'error');
localStorage.setItem('clay-settings', 'not valid JSON');
assert.doesNotThrow(function() {
clay.setSettings('key', 'value');
});
assert(errorStub.calledWithMatch(/SyntaxError/i));
errorStub.restore();
});
});
describe('.meta', function() {
var emptyMeta = {
activeWatchInfo: null,
+21
View File
@@ -15,6 +15,7 @@ describe('ClayConfig', function() {
'getItemByMessageKey',
'getItemById',
'getItemsByType',
'getItemsByGroup',
'serialize',
'registerComponent',
'build',
@@ -224,6 +225,7 @@ describe('ClayConfig', function() {
'getItemByMessageKey',
'getItemById',
'getItemsByType',
'getItemsByGroup',
'serialize'
].forEach(function(method) {
it('.' + method + '()', function() {
@@ -287,6 +289,25 @@ describe('ClayConfig', function() {
});
});
describe('.getItemsByGroup()', function() {
it('it returns the correct items', function() {
var config = fixtures.config([
{type: 'input', id: 'g1-0', group: 'group1'},
{type: 'text', id: 'g2-0', group: 'group2'},
{type: 'input', id: 'g1-1', group: 'group1'}
]);
var clayConfig = fixtures.clayConfig(config);
assert.deepEqual(clayConfig.getItemsByGroup('group1'), [
clayConfig.getItemById('g1-0'),
clayConfig.getItemById('g1-1')
]);
assert.deepEqual(clayConfig.getItemsByGroup('group2'), [
clayConfig.getItemById('g2-0')
]);
});
});
describe('.serialize()', function() {
it('returns the correct settings', function() {
var config = [