Skip to content

Commit

Permalink
Merge pull request #921 from orbitjs/validator-settings
Browse files Browse the repository at this point in the history
Ensure that validator settings are properly transferred to caches and forks
  • Loading branch information
dgeb authored Jan 30, 2022
2 parents e891ceb + 208775c commit 733a031
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 2 deletions.
2 changes: 2 additions & 0 deletions packages/@orbit/indexeddb/src/indexeddb-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,10 @@ export class IndexedDBSource<
cacheSettings.defaultQueryOptions ??= settings.defaultQueryOptions;
cacheSettings.defaultTransformOptions ??= settings.defaultTransformOptions;
cacheSettings.namespace ??= settings.namespace;
cacheSettings.autoValidate ??= settings.autoValidate;

if (
cacheSettings.autoValidate !== false &&
cacheSettings.validatorFor === undefined &&
cacheSettings.validators === undefined
) {
Expand Down
19 changes: 19 additions & 0 deletions packages/@orbit/indexeddb/test/indexeddb-source-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,25 @@ module('IndexedDBSource', function (hooks) {
);
});

test('will not create a `validatorFor` fn if `autoValidate: false`', function (assert) {
const source = new IndexedDBSource({
schema,
keyMap,
autoActivate: false,
autoValidate: false
});
assert.strictEqual(
source.validatorFor,
undefined,
'validatorFor is undefined'
);
assert.strictEqual(
source.cache.validatorFor,
undefined,
'cache.validatorFor is undefined'
);
});

module('activated', function (hooks) {
hooks.beforeEach(async () => {
source = new IndexedDBSource({ schema, keyMap });
Expand Down
2 changes: 2 additions & 0 deletions packages/@orbit/local-storage/src/local-storage-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,10 @@ export class LocalStorageSource<
cacheSettings.defaultTransformOptions ??= settings.defaultTransformOptions;
cacheSettings.namespace ??= settings.namespace;
cacheSettings.delimiter ??= settings.delimiter;
cacheSettings.autoValidate ??= settings.autoValidate;

if (
cacheSettings.autoValidate !== false &&
cacheSettings.validatorFor === undefined &&
cacheSettings.validators === undefined
) {
Expand Down
19 changes: 19 additions & 0 deletions packages/@orbit/local-storage/test/local-storage-source-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,25 @@ module('LocalStorageSource', function (hooks) {
);
});

test('will not create a `validatorFor` fn if `autoValidate: false`', function (assert) {
const source = new LocalStorageSource({
schema,
keyMap,
autoActivate: false,
autoValidate: false
});
assert.strictEqual(
source.validatorFor,
undefined,
'validatorFor is undefined'
);
assert.strictEqual(
source.cache.validatorFor,
undefined,
'cache.validatorFor is undefined'
);
});

test('#getKeyForRecord returns the local storage key that will be used for a record', function (assert) {
assert.equal(
source.getKeyForRecord({ type: 'planet', id: 'jupiter' }),
Expand Down
8 changes: 7 additions & 1 deletion packages/@orbit/memory/src/memory-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,15 @@ export class MemoryCache<
// customizable settings
settings.queryBuilder ??= this._queryBuilder;
settings.transformBuilder ??= this._transformBuilder;
settings.validatorFor ??= this._validatorFor;
settings.defaultQueryOptions ??= this._defaultQueryOptions;
settings.defaultTransformOptions ??= this._defaultTransformOptions;
settings.validatorFor ??= this._validatorFor;
if (
settings.autoValidate === undefined &&
settings.validatorFor === undefined
) {
settings.autoValidate = false;
}

return new MemoryCache(
settings as MemoryCacheSettings<QO, TO, QB, TB, QRD, TRD>
Expand Down
10 changes: 9 additions & 1 deletion packages/@orbit/memory/src/memory-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,10 @@ export class MemorySource<
cacheSettings.transformBuilder ??= this.transformBuilder;
cacheSettings.defaultQueryOptions ??= this.defaultQueryOptions;
cacheSettings.defaultTransformOptions ??= this.defaultTransformOptions;
cacheSettings.autoValidate ??= settings.autoValidate;

if (
cacheSettings.autoValidate !== false &&
cacheSettings.validatorFor === undefined &&
cacheSettings.validators === undefined
) {
Expand Down Expand Up @@ -278,9 +280,15 @@ export class MemorySource<
// customizable settings
settings.queryBuilder ??= this._queryBuilder;
settings.transformBuilder ??= this._transformBuilder;
settings.validatorFor ??= this._validatorFor;
settings.defaultQueryOptions ??= this._defaultQueryOptions;
settings.defaultTransformOptions ??= this._defaultTransformOptions;
settings.validatorFor ??= this._validatorFor;
if (
settings.autoValidate === undefined &&
settings.validatorFor === undefined
) {
settings.autoValidate = false;
}

return new MemorySource<QO, TO, QB, TB, QRD, TRD>(
settings as MemorySourceSettings<QO, TO, QB, TB, QRD, TRD>
Expand Down
27 changes: 27 additions & 0 deletions packages/@orbit/memory/test/memory-cache-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@ module('MemoryCache', function (hooks) {
assert.ok(cache.transformBuilder, 'transformBuilder has been instantiated');
});

test('will not create a `validatorFor` fn if `autoValidate: false`', function (assert) {
let cache = new MemoryCache({ schema, autoValidate: false });
assert.strictEqual(
cache.validatorFor,
undefined,
'cache.validatorFor is undefined'
);
});

test('will track update operations by default if a `base` cache is passed', function (assert) {
let base = new MemoryCache({ schema });
assert.strictEqual(base.base, undefined, 'base.base is undefined');
Expand Down Expand Up @@ -2620,6 +2629,24 @@ module('MemoryCache', function (hooks) {
);
});

test('#fork - skips creating validatorFor if none is set for the base source', async function (assert) {
const cache = new MemoryCache({ schema, keyMap, autoValidate: false });

assert.strictEqual(
cache.validatorFor,
undefined,
'cache.validatorFor is undefined'
);

const fork = cache.fork();

assert.strictEqual(
fork.validatorFor,
undefined,
'fork.validatorFor is undefined'
);
});

test('#merge - merges changes from a forked cache back into a base cache', function (assert) {
const cache = new MemoryCache({ schema, keyMap });

Expand Down
42 changes: 42 additions & 0 deletions packages/@orbit/memory/test/memory-source-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,20 @@ module('MemorySource', function (hooks) {
);
});

test('will not create a `validatorFor` fn if `autoValidate: false`', function (assert) {
const source = new MemorySource({ schema, keyMap, autoValidate: false });
assert.strictEqual(
source.validatorFor,
undefined,
'validatorFor is undefined'
);
assert.strictEqual(
source.cache.validatorFor,
undefined,
'cache.validatorFor is undefined'
);
});

test('#sync - appends transform to log', async function (assert) {
const source = new MemorySource({ schema, keyMap });
const recordA = {
Expand Down Expand Up @@ -502,6 +516,34 @@ module('MemorySource', function (hooks) {
);
});

test('#fork - skips creating validatorFor if none is set for the base source', async function (assert) {
const source = new MemorySource({ schema, keyMap, autoValidate: false });

assert.strictEqual(
source.validatorFor,
undefined,
'source.validatorFor is undefined'
);
assert.strictEqual(
source.cache.validatorFor,
undefined,
'source.cache.validatorFor is undefined'
);

const fork = source.fork();

assert.strictEqual(
fork.validatorFor,
undefined,
'fork.validatorFor is undefined'
);
assert.strictEqual(
fork.cache.validatorFor,
undefined,
'fork.cache.validatorFor is undefined'
);
});

test('#merge - merges changes from a forked source back into a base source', async function (assert) {
const source = new MemorySource({ schema, keyMap });

Expand Down

0 comments on commit 733a031

Please sign in to comment.