Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
window.requestAnimationFrame(() => {
var model = new Schmackbone.Model({key, ...(options.data || {})});
if (options.prefetch) {
haveCalledPrefetch = true;
// never-resolving promise to mock long-loading request
if (prefetchLoading) {
return false;
} else if (prefetchError) {
return rej(model);
}
ModelCache.put(key, model, options.component);
res(model);
} else {
if (/searchQuery/.test(key) && searchQueryLoading) {
haveCalledSearchQuery = true;
it('updates previously existing models if they exist', async(done) => {
var testModel = new Schmackbone.Model({content_abuse: {id: 'another_content_decision'}}),
testKey = 'decisionInstanceentityId=noah_entityType=content';
ModelMap[ResourceKeys.USER].providesModels = () => newProvides;
ModelCache.put(testKey, testModel);
ModelCache.put.calls.reset();
expect(ModelCache.get(testKey).get('content_abuse').id).toEqual('another_content_decision');
({dataChild, dataCarrier} = getRenderedResourceComponents(renderWithResources()));
await waitsFor(() => dataChild.props.hasLoaded);
expect(ModelCache.put.calls.count()).toEqual(4);
// model cache is called, but never for our existing model
expect([
ModelCache.put.calls.argsFor(0)[0],
ModelCache.put.calls.argsFor(1)[0],
it('does not fetch resources that are passed in via props', () => {
resources = renderWithResources({
userModel: new Schmackbone.Model(),
analystsCollection: new Schmackbone.Collection(),
decisionsCollection: new Schmackbone.Collection()
});
expect(requestSpy).not.toHaveBeenCalled();
ReactDOM.unmountComponentAtNode(jasmineNode);
// the models passed down are not fetched
resources = renderWithResources({
userModel: new Schmackbone.Model(),
decisionsCollection: new Schmackbone.Collection()
});
expect(requestSpy.calls.count()).toEqual(1);
expect(requestSpy.calls.mostRecent().args[0]).toEqual('analysts');
});
it('does not fetch resources that are passed in via props', () => {
resources = renderWithResources({
userModel: new Schmackbone.Model(),
analystsCollection: new Schmackbone.Collection(),
decisionsCollection: new Schmackbone.Collection()
});
expect(requestSpy).not.toHaveBeenCalled();
ReactDOM.unmountComponentAtNode(jasmineNode);
// the models passed down are not fetched
resources = renderWithResources({
userModel: new Schmackbone.Model(),
decisionsCollection: new Schmackbone.Collection()
});
expect(requestSpy.calls.count()).toEqual(1);
expect(requestSpy.calls.mostRecent().args[0]).toEqual('analysts');
});
it('does not fetch resources that are passed in via props', () => {
resources = renderWithResources({
userModel: new Schmackbone.Model(),
analystsCollection: new Schmackbone.Collection(),
decisionsCollection: new Schmackbone.Collection()
});
expect(requestSpy).not.toHaveBeenCalled();
ReactDOM.unmountComponentAtNode(jasmineNode);
// the models passed down are not fetched
resources = renderWithResources({
userModel: new Schmackbone.Model(),
decisionsCollection: new Schmackbone.Collection()
});
expect(requestSpy.calls.count()).toEqual(1);
expect(requestSpy.calls.mostRecent().args[0]).toEqual('analysts');
});
spyOn(Schmackbone.Model.prototype, 'fetch').and.callFake(function(options) {
if (waitSuccess) {
// use this to ensure that a model gets removed from the loadingCache
// before other synchronous actions take place
window.requestAnimationFrame(() => {
if (reject) {
options.error(this, {status: 404});
} else {
options.success(this, {}, {response: {status: 200}});
}
});
} else if (typeof options.success === 'function') {
options.success(new Schmackbone.Model(), '', {response: {status: 200}});
}
});
requestSpy = spyOn(Request, 'default').and.callFake((key, Model, options) => {
// mock fetch model cache behavior, where we put it in the cache immediately,
// then request the model, and only if it errors do we remove it from cache
var model = new Schmackbone.Model({key, ...(options.data || {})});
ModelCache.put(key, model, options.component);
return new Promise((res, rej) => {
if ((options.options || {}).delay) {
return window.setTimeout(() => {
delayedResourceComplete = true;
ModelCache.remove(key);
rej(model);
}, options.options.delay);
}
if (cached) {
// treat model as though it were cached by resolving promise immediately
return res(model);
}
import _ from 'underscore';
import _prefetch from './prefetch';
import ErrorBoundary from './error-boundary';
import {LoadingStates} from './constants';
import ModelCache from './model-cache';
import React from 'react';
import request from './request';
import Schmackbone from 'schmackbone';
import schmackboneMixin from './schmackbone-mixin';
const SPREAD_PROVIDES_CHAR = '_';
// pending and errored resources are not cached, but instead of passing down an
// undefined prop, we pass down empty models for greater defense in client code
export const EMPTY_MODEL = Object.freeze(new Schmackbone.Model());
export const EMPTY_COLLECTION = Object.freeze(new Schmackbone.Collection());
// ensure that no withResources client can modify empty models' data
Object.freeze(EMPTY_MODEL.attributes);
Object.freeze(EMPTY_COLLECTION.models);
/**
* This HOC is a light wrapper around the DataCarrier component for setting
* state that should trigger resource updates. Some things won't need this, ie
* a url update that passes its query params down as props[queryParamsPropName].
* But it provides a setResourceState method to wrap any necessary state that
* may cause a resource update in DataCarrier's componentWillReceiveProps.
*/
const resourceState = (Component) =>
class ResourceStateWrapper extends React.Component {
constructor() {
import _ from 'underscore';
import _prefetch from './prefetch';
import ErrorBoundary from './error-boundary';
import {LoadingStates} from './constants';
import ModelCache from './model-cache';
import React from 'react';
import request from './request';
import Schmackbone from 'schmackbone';
import schmackboneMixin from './schmackbone-mixin';
const SPREAD_PROVIDES_CHAR = '_';
// pending and errored resources are not cached, but instead of passing down an
// undefined prop, we pass down empty models for greater defense in client code
export const EMPTY_MODEL = Object.freeze(new Schmackbone.Model());
export const EMPTY_COLLECTION = Object.freeze(new Schmackbone.Collection());
// ensure that no withResources client can modify empty models' data
Object.freeze(EMPTY_MODEL.attributes);
Object.freeze(EMPTY_COLLECTION.models);
/**
* This HOC is a light wrapper around the DataCarrier component for setting
* state that should trigger resource updates. Some things won't need this, ie
* a url update that passes its query params down as props[queryParamsPropName].
* But it provides a setResourceState method to wrap any necessary state that
* may cause a resource update in DataCarrier's componentWillReceiveProps.
*/
const resourceState = (Component) =>
class ResourceStateWrapper extends React.Component {
constructor() {
super();
it('when calling \'existsInCache\' returns true if the model exists in the model cache', () => {
expect(existsInCache('foo')).toBe(false);
ModelCache.put('foo', new Schmackbone.Model(), component);
expect(existsInCache('foo')).toBe(true);
});