Skip to content

Commit

Permalink
correct behavior for multi-client & tests
Browse files Browse the repository at this point in the history
  • Loading branch information
iartemiev committed Feb 19, 2021
1 parent b895932 commit a51d735
Show file tree
Hide file tree
Showing 4 changed files with 433 additions and 5 deletions.
302 changes: 301 additions & 1 deletion packages/datastore/__tests__/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ModelInit, MutableModel, Schema } from '../src/types';
import { ModelInit, MutableModel, Schema, InternalSchema } from '../src/types';

export declare class Model {
public readonly id: string;
Expand Down Expand Up @@ -163,3 +163,303 @@ export function testSchema(): Schema {
version: '1',
};
}

export function internalTestSchema(): InternalSchema {
return {
namespaces: {
datastore: {
name: 'datastore',
relationships: {
Setting: {
indexes: [],
relationTypes: [],
},
},
enums: {},
nonModels: {},
models: {
Setting: {
name: 'Setting',
pluralName: 'Settings',
syncable: false,
fields: {
id: {
name: 'id',
type: 'ID',
isRequired: true,
isArray: false,
},
key: {
name: 'key',
type: 'String',
isRequired: true,
isArray: false,
},
value: {
name: 'value',
type: 'String',
isRequired: true,
isArray: false,
},
},
},
},
},
user: {
name: 'user',
enums: {},
models: {
Model: {
name: 'Model',
pluralName: 'Models',
syncable: true,
fields: {
id: {
name: 'id',
isArray: false,
type: 'ID',
isRequired: true,
},
field1: {
name: 'field1',
isArray: false,
type: 'String',
isRequired: true,
},
optionalField1: {
name: 'optionalField1',
isArray: false,
type: 'String',
isRequired: false,
},
dateCreated: {
name: 'dateCreated',
isArray: false,
type: 'AWSDateTime',
isRequired: true,
attributes: [],
},
emails: {
name: 'emails',
isArray: true,
type: 'AWSEmail',
isRequired: true,
attributes: [],
isArrayNullable: true,
},
ips: {
name: 'ips',
isArray: true,
type: 'AWSIPAddress',
isRequired: false,
attributes: [],
isArrayNullable: true,
},
metadata: {
name: 'metadata',
isArray: false,
type: {
nonModel: 'Metadata',
},
isRequired: false,
attributes: [],
},
},
},
LocalModel: {
name: 'LocalModel',
pluralName: 'LocalModels',
syncable: false,
fields: {
id: {
name: 'id',
isArray: false,
type: 'ID',
isRequired: true,
},
field1: {
name: 'field1',
isArray: false,
type: 'String',
isRequired: true,
},
},
},
},
nonModels: {
Metadata: {
name: 'Metadata',
fields: {
author: {
name: 'author',
isArray: false,
type: 'String',
isRequired: true,
attributes: [],
},
tags: {
name: 'tags',
isArray: true,
type: 'String',
isRequired: false,
isArrayNullable: true,
attributes: [],
},
rewards: {
name: 'rewards',
isArray: true,
type: 'String',
isRequired: true,
attributes: [],
},
penNames: {
name: 'penNames',
isArray: true,
type: 'String',
isRequired: true,
isArrayNullable: true,
attributes: [],
},
nominations: {
name: 'nominations',
isArray: true,
type: 'String',
isRequired: false,
attributes: [],
},
misc: {
name: 'misc',
isArray: true,
type: 'String',
isRequired: false,
isArrayNullable: true,
attributes: [],
},
},
},
},
relationships: {
Model: {
indexes: [],
relationTypes: [],
},
LocalModel: {
indexes: [],
relationTypes: [],
},
},
},
sync: {
name: 'sync',
relationships: {
MutationEvent: {
indexes: [],
relationTypes: [],
},
ModelMetadata: {
indexes: [],
relationTypes: [],
},
},
enums: {
OperationType: {
name: 'OperationType',
values: ['CREATE', 'UPDATE', 'DELETE'],
},
},
nonModels: {},
models: {
MutationEvent: {
name: 'MutationEvent',
pluralName: 'MutationEvents',
syncable: false,
fields: {
id: {
name: 'id',
type: 'ID',
isRequired: true,
isArray: false,
},
model: {
name: 'model',
type: 'String',
isRequired: true,
isArray: false,
},
data: {
name: 'data',
type: 'String',
isRequired: true,
isArray: false,
},
modelId: {
name: 'modelId',
type: 'String',
isRequired: true,
isArray: false,
},
operation: {
name: 'operation',
type: {
enum: 'Operationtype',
},
isArray: false,
isRequired: true,
},
condition: {
name: 'condition',
type: 'String',
isArray: false,
isRequired: true,
},
},
},
ModelMetadata: {
name: 'ModelMetadata',
pluralName: 'ModelsMetadata',
syncable: false,
fields: {
id: {
name: 'id',
type: 'ID',
isRequired: true,
isArray: false,
},
namespace: {
name: 'namespace',
type: 'String',
isRequired: true,
isArray: false,
},
model: {
name: 'model',
type: 'String',
isRequired: true,
isArray: false,
},
lastSync: {
name: 'lastSync',
type: 'Int',
isRequired: false,
isArray: false,
},
lastFullSync: {
name: 'lastFullSync',
type: 'Int',
isRequired: false,
isArray: false,
},
fullSyncInterval: {
name: 'fullSyncInterval',
type: 'Int',
isRequired: true,
isArray: false,
},
},
},
},
},
},
version: '1',
};
}
83 changes: 83 additions & 0 deletions packages/datastore/__tests__/outbox.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import 'fake-indexeddb/auto';
import {
DataStore as DataStoreType,
initSchema as initSchemaType,
} from '../src/datastore/datastore';
import { ExclusiveStorage as StorageType } from '../src/storage/storage';

import { MutationEventOutbox } from '../src/sync/outbox';

import { Model, testSchema, internalTestSchema } from './helpers';

let initSchema: typeof initSchemaType;
// any in order to access private properties
let DataStore: any;
let Storage: typeof StorageType;
let anyStorage: any;

let outbox: MutationEventOutbox;

import { PersistentModelConstructor } from '../src/types';

const ownSymbol = Symbol('sync');

const MutationEvent = {}['MutationEvent'] as PersistentModelConstructor<any>;

// const outbox = new MutationEventOutbox(
// internalTestSchema(),
// null,
// MutationEvent,
// ownSymbol
// );

describe('Outbox tests', () => {
beforeAll(async () => {
({ initSchema, DataStore } = require('../src/datastore/datastore'));
const classes = initSchema(testSchema());

const { Model } = classes as {
Model: PersistentModelConstructor<Model>;
};

await DataStore.start();

Storage = <any>DataStore.storage;

outbox = new MutationEventOutbox(
internalTestSchema(),
null,
MutationEvent,
ownSymbol
);
});

test('blagh', () => {
// outbox.enqueue(Storage, )
expect(true).toBeTruthy();
});
});

const data = {
name: 'Title F - 16:22:17',
id: 'c4e457de-cfa6-49e9-84c4-48e3338ace26',
_version: 727,
_lastChangedAt: 1613596937293,
_deleted: null,
};

const mutationEvent = {
condition: '{}',
data: JSON.stringify(data),
id: '01EYRTP6B2R7AKMS5BJ6BRJPJS',
model: 'Todo',
modelId: 'c4e457de-cfa6-49e9-84c4-48e3338ace26',
operation: 'Update',
};

const response = {
id: 'c4e457de-cfa6-49e9-84c4-48e3338ace26',
name: 'Title Z - 16:29:51',
_version: 747,
_lastChangedAt: 1613597392344,
_deleted: null,
};
Loading

0 comments on commit a51d735

Please sign in to comment.