Skip to content

Commit

Permalink
Add getBookmarkedSpecs API and test
Browse files Browse the repository at this point in the history
  • Loading branch information
abanh206 authored and FelixCodes committed Feb 5, 2018
1 parent 52e1489 commit 5587241
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
51 changes: 48 additions & 3 deletions src/lib-voyager.test.ui.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

import * as ReactDOM from 'react-dom';
import {CreateVoyager} from './lib-voyager';
import {SerializableState} from './models/index';

import {Bookmark, DEFAULT_BOOKMARK} from './models/bookmark';
import {
DEFAULT_PERSISTENT_STATE, DEFAULT_UNDOABLE_STATE,
SerializableState, State, toSerializable
} from './models/index';
const DEFAULT_TIMEOUT_LENGTH = 300;

describe('lib-voyager', () => {
Expand Down Expand Up @@ -145,6 +148,49 @@ describe('lib-voyager', () => {

});

describe('getBookmarkedSpecs', () => {
it('returns bookmarked vega-lite specs', done => {
setTimeout(() => {
try {
const spec1 = '{"data":{"name":"source"},"mark":"bar","encoding":{"y":{"field":"Name","type":"nominal",'
+ '"scale":{"rangeStep":12}}, "x":{"aggregate":"count","field":"*","type":"quantitative",'
+ '"axis":{"orient":"top"}}}, "config":{"overlay":{"line":true},"scale":{"useUnaggregatedDomain":true}}}';
const spec2 = '{"data":{"name":"source"},"mark":"bar","encoding":{"y":{"field":"Cylinders","type":"nominal"},'
+ '"x":{"aggregate":"count","field":"*","type":"quantitative"}},'
+ '"config":{"overlay":{"line":true},"scale":{"useUnaggregatedDomain":true}}}';
const voyagerInst = CreateVoyager(container, undefined, undefined);
const customBookmarks: Bookmark = {
...DEFAULT_BOOKMARK,
list: [spec1, spec2]
};

const customState: State = {
persistent: {
...DEFAULT_PERSISTENT_STATE,
bookmark: customBookmarks
},
undoable: {...DEFAULT_UNDOABLE_STATE}
};

voyagerInst.setApplicationState(toSerializable(customState));

setTimeout(() => {
try {
const bookmarkedSpecs: string[] = voyagerInst.getBookmarkedSpecs();
expect(bookmarkedSpecs).toEqual([spec1, spec2]);
done();
} catch (err) {
done.fail(err);
}
}, DEFAULT_TIMEOUT_LENGTH);

} catch (err) {
done.fail(err);
}

}, DEFAULT_TIMEOUT_LENGTH);
});
});

describe('vega-lite spec', () => {
it('accepts valid spec', done => {
Expand Down Expand Up @@ -376,5 +422,4 @@ describe('lib-voyager', () => {

}, DEFAULT_TIMEOUT_LENGTH);
});

});
16 changes: 15 additions & 1 deletion src/lib-voyager.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import * as draft4Schemas from 'ajv/lib/refs/json-schema-draft-04.json';
import 'font-awesome-sass-loader'; // TODO should this move to App?
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { Provider } from 'react-redux';
import {Provider} from 'react-redux';
import {Store} from 'redux';
import {Data} from 'vega-lite/build/src/data';
import {FacetedCompositeUnitSpec, isUnitSpec, TopLevel, TopLevelExtendedSpec} from 'vega-lite/build/src/spec';
Expand All @@ -21,10 +21,12 @@ import {State} from './models';
import {DEFAULT_VOYAGER_CONFIG, VoyagerConfig} from './models/config';
import {fromSerializable, SerializableState, toSerializable} from './models/index';
import {selectData} from './selectors/dataset';
import {selectBookmark} from './selectors/index';
import {selectMainSpec} from './selectors/result';
import {configureStore} from './store';



export type Container = string | HTMLElement;

/**
Expand Down Expand Up @@ -169,6 +171,18 @@ export class Voyager {
return spec;
}

/**
*
* Gets the current bookmarked vega-lite specs.
*
* @returns {string[]}
*
* @memberof Voyager
*/
public getBookmarkedSpecs(): string[] {
return selectBookmark(this.store.getState()).list;
}

/**
* Subscribe to state changes.
*
Expand Down

0 comments on commit 5587241

Please sign in to comment.