Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add multi-expression/operation tests for jsonapi #792

Merged
merged 1 commit into from Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
125 changes: 113 additions & 12 deletions packages/@orbit/jsonapi/test/jsonapi-source-pullable-test.ts
Expand Up @@ -13,10 +13,10 @@ import * as sinon from 'sinon';
import { SinonStub } from 'sinon';
import {
JSONAPIResourceIdentitySerializer,
JSONAPIResourceSerializer
JSONAPIResourceSerializer,
Resource
} from '../src';
import { JSONAPISource } from '../src/jsonapi-source';
import { Resource } from '../src/resources';
import { JSONAPISerializers } from '../src/serializers/jsonapi-serializers';
import { jsonapiResponse } from './support/jsonapi';
import {
Expand Down Expand Up @@ -1630,14 +1630,115 @@ module('JSONAPISource - pullable', function (hooks) {
});
});

// TODO
// module('with no secondary keys', function (hooks) {
// hooks.beforeEach(function () {
// let schema = createSchemaWithoutKeys();
// source = new JSONAPISource({ schema });
// resourceSerializer = source.requestProcessor.serializerFor(
// JSONAPISerializers.Resource
// ) as JSONAPIResourceSerializer;
// });
// });
module('with no secondary keys', function (hooks) {
hooks.beforeEach(function () {
let schema = createSchemaWithoutKeys();
source = new JSONAPISource({ schema });
resourceSerializer = source.requestProcessor.serializerFor(
JSONAPISerializers.Resource
) as JSONAPIResourceSerializer;
});

test('#pull - one expression', async function (assert) {
assert.expect(3);

const planetsDoc = {
data: [
{
type: 'planet',
id: 'p1',
attributes: { name: 'Jupiter' }
},
{
type: 'planet',
id: 'p2',
attributes: { name: 'Earth' }
}
]
};

fetchStub.withArgs('/planets').returns(jsonapiResponse(200, planetsDoc));

let transforms = await source.pull((q) => [q.findRecords('planet')]);

assert.equal(transforms.length, 1, 'one transform returned');

assert.deepEqual(
transforms[0].operations.map((o) => o.op),
['updateRecord', 'updateRecord']
);
assert.deepEqual(
transforms[0].operations.map(
(o) => (o as UpdateRecordOperation).record.attributes?.name
),
['Jupiter', 'Earth']
);
});

test('#pull - can query multiple expressions in series', async function (assert) {
assert.expect(5);

const planetsDoc = {
data: [
{
type: 'planet',
id: 'p1',
attributes: { name: 'Jupiter' }
},
{
type: 'planet',
id: 'p2',
attributes: { name: 'Earth' }
}
]
};

const moonsDoc = {
data: [
{
type: 'moon',
id: 'm1',
attributes: { name: 'Io' }
},
{
type: 'moon',
id: 'm2',
attributes: { name: 'Europa' }
}
]
};

fetchStub.withArgs('/planets').returns(jsonapiResponse(200, planetsDoc));
fetchStub.withArgs('/moons').returns(jsonapiResponse(200, moonsDoc));

let transforms = await source.pull((q) => [
q.findRecords('planet'),
q.findRecords('moon')
]);

assert.equal(transforms.length, 2, 'two transforms returned');

assert.deepEqual(
transforms[0].operations.map((o) => o.op),
['updateRecord', 'updateRecord']
);
assert.deepEqual(
transforms[0].operations.map(
(o) => (o as UpdateRecordOperation).record.attributes?.name
),
['Jupiter', 'Earth']
);

assert.deepEqual(
transforms[1].operations.map((o) => o.op),
['updateRecord', 'updateRecord']
);
assert.deepEqual(
transforms[1].operations.map(
(o) => (o as UpdateRecordOperation).record.attributes?.name
),
['Io', 'Europa']
);
});
});
});
71 changes: 71 additions & 0 deletions packages/@orbit/jsonapi/test/jsonapi-source-pushable-test.ts
Expand Up @@ -1060,6 +1060,77 @@ module('JSONAPISource - pushable', function (hooks) {
);
});

test('#push - can add multiple records in series', async function (assert) {
assert.expect(8);

let planet1 = {
type: 'planet',
id: 'p1',
attributes: { name: 'Jupiter' }
} as Record;

let moon1 = {
type: 'moon',
id: 'm1',
attributes: { name: 'Io' }
} as Record;

fetchStub.withArgs('/planets').returns(
jsonapiResponse(201, {
data: planet1
})
);
fetchStub.withArgs('/moons').returns(
jsonapiResponse(201, {
data: moon1
})
);

await source.push((t) => [t.addRecord(planet1), t.addRecord(moon1)]);

assert.ok(true, 'push resolves successfully');

assert.equal(fetchStub.callCount, 2, 'fetch called twice');

const firstFetchCall = fetchStub.getCall(0);
assert.equal(
firstFetchCall.args[1].method,
'POST',
'fetch called with expected method'
);
assert.equal(
firstFetchCall.args[1].headers['Content-Type'],
'application/vnd.api+json',
'fetch called with expected content type'
);
assert.deepEqual(
JSON.parse(firstFetchCall.args[1].body),
{
data: planet1
},
'fetch called with expected data'
);

const secondFetchCall = fetchStub.getCall(1);
assert.equal(
secondFetchCall.args[1].method,
'POST',
'fetch called with expected method'
);
assert.equal(
secondFetchCall.args[1].headers['Content-Type'],
'application/vnd.api+json',
'fetch called with expected content type'
);
assert.deepEqual(
JSON.parse(secondFetchCall.args[1].body),
{
data: moon1
},
'fetch called with expected data'
);
});

test('#push - addRecord - url option can be passed', async function (assert) {
assert.expect(1);

Expand Down
62 changes: 62 additions & 0 deletions packages/@orbit/jsonapi/test/jsonapi-source-queryable-test.ts
Expand Up @@ -1506,6 +1506,68 @@ module('JSONAPISource - queryable', function (hooks) {
) as JSONAPIResourceSerializer;
});

test('#query - can query multiple expressions in series', async function (assert) {
assert.expect(9);

const planetsDoc = {
data: [
{
type: 'planet',
id: 'p1',
attributes: { name: 'Jupiter' }
},
{
type: 'planet',
id: 'p2',
attributes: { name: 'Earth' }
}
]
};

const moonsDoc = {
data: [
{
type: 'moon',
id: 'm1',
attributes: { name: 'Io' }
},
{
type: 'moon',
id: 'm2',
attributes: { name: 'Europa' }
}
]
};

fetchStub.withArgs('/planets').returns(jsonapiResponse(200, planetsDoc));
fetchStub.withArgs('/moons').returns(jsonapiResponse(200, moonsDoc));

let [planets, moons] = await source.query((q) => [
q.findRecords('planet'),
q.findRecords('moon')
]);

assert.equal(planets.length, 2, 'multiple planets returned');
assert.equal(planets[0].attributes?.name, 'Jupiter');
assert.equal(planets[1].attributes?.name, 'Earth');

assert.equal(moons.length, 2, 'multiple moons returned');
assert.equal(moons[0].attributes?.name, 'Io');
assert.equal(moons[1].attributes?.name, 'Europa');

assert.equal(fetchStub.callCount, 2, 'fetch called twice');
assert.equal(
fetchStub.getCall(0).args[1].method,
undefined,
'fetch called with no method (equivalent to GET)'
);
assert.equal(
fetchStub.getCall(1).args[1].method,
undefined,
'fetch called with no method (equivalent to GET)'
);
});

test('#query - findRecord - url option can be passed', async function (assert) {
assert.expect(1);

Expand Down