diff --git a/src/lib-voyager.test.ui.tsx b/src/lib-voyager.test.ui.tsx index 9e0dc78ff..9c0c6d075 100644 --- a/src/lib-voyager.test.ui.tsx +++ b/src/lib-voyager.test.ui.tsx @@ -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', () => { @@ -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 => { @@ -376,5 +422,4 @@ describe('lib-voyager', () => { }, DEFAULT_TIMEOUT_LENGTH); }); - }); diff --git a/src/lib-voyager.tsx b/src/lib-voyager.tsx index 62b12e26c..f0353c72d 100644 --- a/src/lib-voyager.tsx +++ b/src/lib-voyager.tsx @@ -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'; @@ -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; /** @@ -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. *