import test from 'ava';
import Resource from '../src/Resource';
import ResourceArray from '../src/ResourceArray';
import applyHooks from './helpers/applyHooks';
import { model } from '../src';

applyHooks(test);

test('should create a user and sync it\'s relationships', async t => {
  try {
    const user = await model('user').create({
      name: 'CJ',
      friends: ['1'],
      company: '1',
    });

    const employees = await model('company').fetchResource('1')
      .then(res => res.fetch('employees'));

    const friends = await model('user').fetchResource('1')
      .then(res => res.fetch('friends'));

    t.truthy(employees instanceof ResourceArray);
    t.truthy(friends instanceof ResourceArray);
    t.truthy(user instanceof Resource);

    t.is(user.attribute('name'), 'CJ');
    t.is(employees.toArray().length, 2);
    t.is(friends.toArray().length, 2);
  } catch (err) {
    t.fail(err.message);
  }
});

test('should fail to create a user because the company doesn\'t exist', async t => {
  try {
    await model('user').create({
      name: 'CJ',
      blogs: [],
      friends: ['1'],
      company: 'invalid',
    });
  } catch (err) {
    t.is(
      err.message,
      'Expected a valid record of type \'company\' but got invalid record with id of \'invalid\'.'
    );
  }
});

@@ -1292,16 +1292,430 @@
 %0A    );%0A  %7D%0A%7D);%0A
+%0Atest('should create a blog that belongs to a user', async t =%3E %7B%0A  try %7B%0A    const blog = await model('blog').create(%7B%0A      title: 'Super Cool Blog',%0A      author: '1',%0A    %7D);%0A%0A    const user = await model('user').fetchResource('1', %7B%0A      include: %7B blogs: true %7D,%0A    %7D);%0A%0A    t.truthy(blog instanceof Resource);%0A    t.is(user.retrieve('blogs').length, 2);%0A  %7D catch (err) %7B%0A    t.fail(err.message);%0A  %7D%0A%7D);%0A

Add `belongsTo` test