Skip to content

Commit

Permalink
fix(@aws-amplify/datastore): Make save return a single model instead …
Browse files Browse the repository at this point in the history
  • Loading branch information
manueliglesias authored Mar 26, 2020
1 parent 05f1b54 commit 1d0b8e1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
40 changes: 35 additions & 5 deletions packages/datastore/__tests__/DataStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import Observable from 'zen-observable-ts';

let initSchema: typeof initSchemaType;
let DataStore: typeof DataStoreType;
let Storage: typeof StorageType;

beforeEach(() => {
jest.resetModules();
Expand Down Expand Up @@ -192,7 +191,7 @@ describe('DataStore tests', () => {

describe('Initialization', () => {
test('start is called only once', async () => {
Storage = require('../src/storage/storage').default;
const storage: StorageType = require('../src/storage/storage').default;

const classes = initSchema(testSchema());

Expand All @@ -207,19 +206,49 @@ describe('DataStore tests', () => {

await Promise.all(promises);

expect(Storage).toHaveBeenCalledTimes(1);
expect(storage).toHaveBeenCalledTimes(1);
});

test('It is initialized when observing (no query)', async () => {
Storage = require('../src/storage/storage').default;
const storage: StorageType = require('../src/storage/storage').default;

const classes = initSchema(testSchema());

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

DataStore.observe(Model).subscribe(jest.fn());

expect(Storage).toHaveBeenCalledTimes(1);
expect(storage).toHaveBeenCalledTimes(1);
});
});

describe('Basic operations', () => {
test('Save returns the saved model', async () => {
let model: Model;

jest.resetModules();
jest.doMock('../src/storage/storage', () => {
const mock = jest.fn().mockImplementation(() => ({
runExclusive: jest.fn(() => [model]),
}));

(<any>mock).getNamespace = () => ({ models: {} });

return { default: mock };
});
({ initSchema, DataStore } = require('../src/datastore/datastore'));

const classes = initSchema(testSchema());

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

model = new Model({
field1: 'Some value',
});

const result = await DataStore.save(model);

expect(result).toMatchObject(model);
});
});

Expand All @@ -244,6 +273,7 @@ describe('DataStore tests', () => {
declare class Model {
public readonly id: string;
public readonly field1: string;
public readonly metadata?: Metadata;

constructor(init: ModelInit<Model>);

Expand Down
10 changes: 5 additions & 5 deletions packages/datastore/__tests__/indexeddb.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ describe('Indexed db storage test', () => {
});

test('query function 1:1', async () => {
const [res] = await DataStore.save(blog);
const res = await DataStore.save(blog);
await DataStore.save(owner);
const query = await DataStore.query(Blog, blog.id);

Expand Down Expand Up @@ -364,8 +364,8 @@ describe('Indexed db storage test', () => {
});

test('delete cascade', async () => {
const [a1] = await DataStore.save(new Author({ name: 'author1' }));
const [a2] = await DataStore.save(new Author({ name: 'author2' }));
const a1 = await DataStore.save(new Author({ name: 'author1' }));
const a2 = await DataStore.save(new Author({ name: 'author2' }));
const blog = new Blog({
name: 'The Blog',
owner,
Expand All @@ -378,8 +378,8 @@ describe('Indexed db storage test', () => {
title: 'Post 2',
blog,
});
const [c1] = await DataStore.save(new Comment({ content: 'c1', post: p1 }));
const [c2] = await DataStore.save(new Comment({ content: 'c2', post: p1 }));
const c1 = await DataStore.save(new Comment({ content: 'c1', post: p1 }));
const c2 = await DataStore.save(new Comment({ content: 'c2', post: p1 }));
await DataStore.save(p1);
await DataStore.save(p2);
await DataStore.save(blog);
Expand Down
4 changes: 2 additions & 2 deletions packages/datastore/src/datastore/datastore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ const createNonModelClass = <T>(typeDefinition: SchemaNonModel) => {
const save = async <T extends PersistentModel>(
model: T,
condition?: ProducerModelPredicate<T>
) => {
): Promise<T> => {
await start();
const modelConstructor: PersistentModelConstructor<T> = model
? <PersistentModelConstructor<T>>model.constructor
Expand All @@ -374,7 +374,7 @@ const save = async <T extends PersistentModel>(
condition
);

const savedModel = await storage.runExclusive(async s => {
const [savedModel] = await storage.runExclusive(async s => {
await s.save(model, producedCondition);

return s.query(
Expand Down

0 comments on commit 1d0b8e1

Please sign in to comment.