Skip to content
This repository has been archived by the owner on May 24, 2023. It is now read-only.

Commit

Permalink
Fix(EntityUid): Replaced instanceof EntityUid, fix deprecations in …
Browse files Browse the repository at this point in the history
…tests

Replaced `value instanceof EntityUid` by `EntityUid.isEntityUid(value)` for compatibility. `instanceof` implementation did not worked as expected, returning false negative.
Changed `Diaspora.createDataSource` with `Diaspora.createNamedDataSource` in tests.
  • Loading branch information
Gerkin committed Dec 1, 2018
1 parent a7a9d4f commit 8b3f720
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 41 deletions.
4 changes: 2 additions & 2 deletions .vscode/csak-timelog.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"starttime": "9/16/2018, 7:56:26 PM",
"totaltimesec": 169177,
"lasttime": "11/30/2018, 11:36:55 PM"
"totaltimesec": 172748,
"lasttime": "12/1/2018, 11:46:44 PM"
}
29 changes: 7 additions & 22 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 15 additions & 11 deletions src/adapters/dataAccessLayer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,22 @@ export namespace Adapter {
* TODO: Replace with a decorator to register type validation.
* For instance, mongo may use the new decorator to declare a checking class that may recognize a class instance as an entity uid.
* It would allow the mongo adapter to use normal mongo uuid as EntityUid type member
*/
export class EntityUid {}
/**
* Test for an entity to be if it can be an EntityUid
*
* @param tested - Value to test
* @returns True if it is a valid entity Uid, false otherwise.
* @author Gerkin
*/
Object.defineProperty( EntityUid, Symbol.hasInstance, ( tested: any ) =>
_.isString( tested ) || _.isNumber( tested ) );

const isEntityUid = ( query: any ): query is EntityUid => query instanceof EntityUid;
export class EntityUid {
/**
* Use `isEntityUid` to check if the value can be a valid entity uid
*
* @returns True if it is a valid entity Uid, false otherwise.
* @author Gerkin
* @see http://www.ecma-international.org/ecma-262/6.0/#sec-function.prototype-@@hasinstance `Symbol.hasInstance` should defined with `Object.defineProperty`
*/
public static isEntityUid( query: any ): query is EntityUid {
return ( _.isString( query ) && query !== '' ) ||
( _.isNumber( query ) && query !== 0 );
}
}

/**
* The Data Access Layer class is the components that wraps adapter calls to provide standard inputs & outputs. Typically, it casts raw query & raw query options in standard query & standard query options, and casts POJO from the output of the adapter's query in adapter entity.
Expand Down Expand Up @@ -375,7 +379,7 @@ export namespace Adapter {
): QueryLanguage.Raw.SelectQueryOrCondition {
if ( _.isNil( query ) ) {
return {};
} else if ( isEntityUid( query ) ) {
} else if ( EntityUid.isEntityUid( query ) ) {
return {
id: query,
};
Expand Down
6 changes: 3 additions & 3 deletions test/unit-test/adapters/browserWebApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as _ from 'lodash';
import { XMLHttpRequest } from 'xmlhttprequest';

import { Adapter as _WebApiAdapter } from '../../../src/adapters/webApi/adapter';
import WebApiAdapter = _WebApiAdapter.WebApi.AWebApiAdapter
import WebApiAdapter = _WebApiAdapter.WebApi.AWebApiAdapter;
import { Adapter as _BrowserWebApiAdapter } from '../../../src/adapters/webApi/subAdapters/browserAdapter';
import BrowserWebApiAdapter = _BrowserWebApiAdapter.WebApi.BrowserWebApiAdapter;

Expand All @@ -24,9 +24,9 @@ const adapter = dal.adapter as BrowserWebApiAdapter;
beforeAll( async () => {
Diaspora.logger.level = ELoggingLevel.Silent;
const INMEMORY_TABLE = 'test-expressstoreLocal';
const inMemoryAdapter = await Diaspora.createDataSource(
'inMemory',
const inMemoryAdapter = await Diaspora.createNamedDataSource(
INMEMORY_TABLE,
'inMemory',
adapterConfig
).waitReady();
const ENDPOINT = '/api/test';
Expand Down
16 changes: 16 additions & 0 deletions test/unit-test/adapters/data-access-layer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Adapter } from '../../../src/adapters';

describe( 'instanceof EntityUid', () => {
it( 'OK Non empty string', () => {
expect( Adapter.EntityUid.isEntityUid( 'foo' ) ).toBe( true );
expect( Adapter.EntityUid.isEntityUid( '' ) ).toBe( false );
} );
it( 'OK Non 0 integer', () => {
expect( Adapter.EntityUid.isEntityUid( 42 ) ).toBe( true );
expect( Adapter.EntityUid.isEntityUid( 0 ) ).toBe( false );
} );
it( 'Objects should not match', () => {
expect( Adapter.EntityUid.isEntityUid( {} ) ).toBe( false );
expect( Adapter.EntityUid.isEntityUid( [] ) ).toBe( false );
} );
} );
6 changes: 3 additions & 3 deletions test/unit-test/adapters/nodeWebApi.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Adapter as _WebApiAdapter } from '../../../src/adapters/webApi/adapter';
import WebApiAdapter = _WebApiAdapter.WebApi.AWebApiAdapter
import WebApiAdapter = _WebApiAdapter.WebApi.AWebApiAdapter;
import { Adapter as _NodeWebApiAdapter } from '../../../src/adapters/webApi/subAdapters/nodeAdapter';
import NodeWebApiAdapter = _NodeWebApiAdapter.WebApi.NodeWebApiAdapter;

Expand All @@ -19,9 +19,9 @@ const adapter = dal.adapter as NodeWebApiAdapter;
beforeAll( async () => {
Diaspora.logger.level = ELoggingLevel.Silent;
const INMEMORY_TABLE = 'test-expressstoreNode';
const inMemoryAdapter = await Diaspora.createDataSource(
'inMemory',
const inMemoryAdapter = await Diaspora.createNamedDataSource(
INMEMORY_TABLE,
'inMemory',
adapterConfig
).waitReady();
const ENDPOINT = '/api/test';
Expand Down

0 comments on commit 8b3f720

Please sign in to comment.