UNPKG

falcor

Version:

A JavaScript library for efficient data fetching.

281 lines (243 loc) 9.88 kB
var falcor = require("./../../../lib/"); var Model = falcor.Model; var noOp = function() {}; var strip = require('./../../cleanData').stripDerefAndVersionKeys; var cacheGenerator = require('./../../CacheGenerator'); var Cache = require('./../../data/Cache'); var toValue = function(x) { return {value: x}; }; var jsonGraph = require('falcor-json-graph'); var toObservable = require('../../toObs'); describe('Cache Only', function() { describe('toJSON', function() { it('should set a value from falcor.', function(done) { var model = new Model({ cache: cacheGenerator(0, 1) }); var onNext = jest.fn(); toObservable(model. set({path: ['videos', 0, 'title'], value: 'V0'})). doAction(onNext, noOp, function() { expect(onNext).toHaveBeenCalledTimes(1); expect(strip(onNext.mock.calls[0][0])).toEqual({ json: { videos: { 0: { title: 'V0' } } } }); }). subscribe(noOp, done, done); }); it('should correctly output with many different input types.', function(done) { var model = new Model({ cache: cacheGenerator(0, 3) }); var onNext = jest.fn(); toObservable(model. set({ path: ['videos', 0, 'title'], value: 'V0' }, { jsonGraph: { videos: { 1: { title: 'V1' } } }, paths: [['videos', 1, 'title']] }, { json: { videos: { 2: { title: 'V2' } } } })). doAction(onNext, noOp, function() { expect(onNext).toHaveBeenCalledTimes(1); expect(strip(onNext.mock.calls[0][0])).toEqual({ json: { videos: { 0: { title: 'V0' }, 1: { title: 'V1' }, 2: { title: 'V2' } } } }); }). subscribe(noOp, done, done); }); }); describe('_toJSONG', function() { it('should get a value from falcor.', function(done) { var model = new Model({ cache: cacheGenerator(0, 1) }); var onNext = jest.fn(); toObservable(model. set({path: ['videos', 0, 'title'], value: 'V0'}). _toJSONG()). doAction(onNext, noOp, function() { expect(onNext).toHaveBeenCalledTimes(1); expect(strip(onNext.mock.calls[0][0])).toEqual({ jsonGraph: { videos: { 0: { title: 'V0' } } }, paths: [['videos', 0, 'title']] }); }). subscribe(noOp, done, done); }); }); describe('Error Selector (during set)', function() { function generateErrorSelectorSpy(expectedPath) { return jest.fn(function(path, atom) { expect(atom.$type).toBe('error'); expect(atom.value.message).toBe('errormsg'); var o = { $type: atom.$type, $custom: 'custom', value: { message: atom.value.message, customtype: 'customtype' } }; return o; }); } function assertExpectedErrorPayload(e, expectedPath) { var path = e.path; var value = e.value; // To avoid hardcoding/scrubbing $size, and other internals expect(path).toEqual(expectedPath); expect(value.$type).toBe('error'); expect(value.$custom).toBe('custom'); expect(value.value).toEqual({ message: 'errormsg', customtype: 'customtype' }); } it('should get invoked with the right arguments for simple paths', function(done) { var testPath = ['genreList', 0, 0, 'errorPath']; var modelCache = Cache(); var onNextSpy = jest.fn(); var onErrorSpy = jest.fn(); var errorSelectorSpy = generateErrorSelectorSpy(testPath); var model = new Model({ cache : modelCache, errorSelector : errorSelectorSpy }); toObservable(model. boxValues(). setValue(testPath, jsonGraph.error({message:'errormsg'}))). doAction(onNextSpy, onErrorSpy, noOp). subscribe( noOp, function(e) { expect(errorSelectorSpy).toHaveBeenCalledTimes(1); expect(errorSelectorSpy.mock.calls[0][0]).toEqual(testPath); expect(onErrorSpy).toHaveBeenCalledTimes(1); expect(e.length).toBe(1); assertExpectedErrorPayload(e[0], testPath); done(); }, function() { expect(onNextSpy).not.toHaveBeenCalled(); expect(onErrorSpy).toHaveBeenCalledTimes(1); done(); }); }); it('should get invoked with the correct error paths for a keyset', function(done) { var testPath = ['genreList',[0,1],0,'errorPath']; var modelCache = Cache(); var onNextSpy = jest.fn(); var onErrorSpy = jest.fn(); var errorSelectorSpy = generateErrorSelectorSpy(testPath); var model = new Model({ cache: modelCache, errorSelector: errorSelectorSpy }); toObservable(model. boxValues(). set({ path: testPath, value: jsonGraph.error({message:'errormsg'}) })). doAction(onNextSpy, onErrorSpy, noOp). subscribe( noOp, function(e) { expect(errorSelectorSpy).toHaveBeenCalledTimes(2); expect(errorSelectorSpy.mock.calls[0][0]).toEqual(['genreList',0,0,'errorPath']); expect(errorSelectorSpy.mock.calls[1][0]).toEqual(['genreList',1,0,'errorPath']); expect(e.length).toBe(2); assertExpectedErrorPayload(e[0], ['genreList',0,0,'errorPath']); assertExpectedErrorPayload(e[1], ['genreList',1,0,'errorPath']); done(); }, function() { expect(onNextSpy).not.toHaveBeenCalled(); expect(onErrorSpy).toHaveBeenCalledTimes(1); done(); }); }); it('should be allowed to change $type', function(done) { var testPath = ['genreList', 0, 0, 'errorPath']; var modelCache = Cache(); var onNextSpy = jest.fn(); var onErrorSpy = jest.fn(); var model = new Model({ cache : modelCache, errorSelector : function(path, atom) { var o = { $type: 'atom', $custom: 'custom', value: { message: atom.value.message, customtype: 'customtype' } }; return o; } }); toObservable(model. boxValues(). setValue(testPath, jsonGraph.error({message:'errormsg'}))). doAction(onNextSpy, onErrorSpy, noOp). subscribe( noOp, function(e) { expect(onErrorSpy).not.toHaveBeenCalled(); done(); }, function() { expect(onErrorSpy).not.toHaveBeenCalled(); expect(onNextSpy).toHaveBeenCalledTimes(1); expect(onNextSpy.mock.calls[0][0]).toEqual({ $type: 'atom', $custom: 'custom', value: { message: 'errormsg', customtype: 'customtype' }, $size:51 }); done(); }); }); }); });