Skip to content

Commit

Permalink
added testing to ensure outbox only contains expected fields
Browse files Browse the repository at this point in the history
  • Loading branch information
svidgen committed Jun 10, 2022
1 parent 305cc1c commit aebf936
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,12 +120,20 @@ describe('SQLiteAdapter', () => {
return await db.getAll('select * from MutationEvent', []);
}

async function clearOutbox() {
await pause(250);
const adapter = (DataStore as any).storageAdapter;
const db = (adapter as any).db;
return await db.executeStatements(['delete from MutationEvent']);
}

({ initSchema, DataStore } = require('@aws-amplify/datastore'));
addCommonQueryTests({
initSchema,
DataStore,
storageAdapter: SQLiteAdapter,
getMutations,
clearOutbox,
});

describe('something', () => {
Expand Down
7 changes: 7 additions & 0 deletions packages/datastore/__tests__/AsyncStorageAdapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import AsyncStorageAdapter from '../src/storage/adapter/AsyncStorageAdapter';
import {
DataStore as DataStoreType,
initSchema as initSchemaType,
syncClasses,
} from '../src/datastore/datastore';
import { PersistentModelConstructor, SortDirection } from '../src/types';
import { pause, Model, User, Profile, testSchema } from './helpers';
Expand All @@ -23,12 +24,18 @@ describe('AsyncStorageAdapter tests', () => {
return await adapter.getAll('sync_MutationEvent');
}

async function clearOutbox(adapter) {
await pause(250);
return await adapter.delete(syncClasses['MutationEvent']);
}

({ initSchema, DataStore } = require('../src/datastore/datastore'));
addCommonQueryTests({
initSchema,
DataStore,
storageAdapter: AsyncStorageAdapter,
getMutations,
clearOutbox,
});

describe('Query', () => {
Expand Down
7 changes: 7 additions & 0 deletions packages/datastore/__tests__/IndexedDBAdapter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'fake-indexeddb/auto';
import {
DataStore as DataStoreType,
initSchema as initSchemaType,
syncClasses,
} from '../src/datastore/datastore';
import { PersistentModelConstructor, SortDirection } from '../src/types';
import {
Expand All @@ -29,12 +30,18 @@ describe('IndexedDBAdapter tests', () => {
return await adapter.getAll('sync_MutationEvent');
}

async function clearOutbox(adapter) {
await pause(250);
return await adapter.delete(syncClasses['MutationEvent']);
}

({ initSchema, DataStore } = require('../src/datastore/datastore'));
addCommonQueryTests({
initSchema,
DataStore,
storageAdapter: Adapter,
getMutations,
clearOutbox,
});

describe('Query', () => {
Expand Down
26 changes: 26 additions & 0 deletions packages/datastore/__tests__/commonAdapterTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export function addCommonQueryTests({
DataStore,
storageAdapter,
getMutations,
clearOutbox,
}) {
describe('Common `query()` cases', () => {
let Model: PersistentModelConstructor<Model>;
Expand Down Expand Up @@ -341,5 +342,30 @@ export function addCommonQueryTests({
postId: mutations[0].modelId,
});
});

it('only includes changed fields in mutations', async () => {
const profile = await DataStore.save(
new Profile({ firstName: 'original first', lastName: 'original last' })
);

await clearOutbox(adapter);

await DataStore.save(
Profile.copyOf(profile, draft => {
draft.firstName = 'new first';
})
);

const mutations = await getMutations(adapter);

expect(mutations.length).toBe(1);
expectMutation(mutations[0], {
firstName: 'new first',
lastName: undefined,
_version: v => v === undefined || v === null,
_lastChangedAt: v => v === undefined || v === null,
_deleted: v => v === undefined || v === null,
});
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ export class AsyncStorageAdapter implements Adapter {
const nameSpace = this.namespaceResolver(modelConstructor);

const storeName = this.getStorenameForModel(modelConstructor);

if (condition) {
const fromDB = await this.db.get(model.id, storeName);

Expand Down

0 comments on commit aebf936

Please sign in to comment.