test/unit/helpers.location.js
import test from 'ava';
import nock from 'nock';
import {
client,
Location,
} from '../../src';
// See tests.js for testing client creation
test.before('start server', () => {
client.apiBaseUrl = 'https://somesite.Clinical6.com';
client.authToken = 'valid_token';
});
test.beforeEach((t) => {
client.cache = 'never';
client.authToken = 'valid_token';
t.context.insertResponse = {
data: {
id: 143,
type: 'locations',
attributes: {
country: 'USA',
state: 'CA',
city: 'San Diego',
latitude: null,
longitude: null,
title: 'Location Title',
zip_code: null,
address_line_1: null,
address_line_2: null,
address_line_3: null
}
}
};
t.context.locationJsonApi = {
type: 'locations',
attributes: {
title: 'Location Title',
city: 'San Diego',
state: 'CA',
country: 'USA',
address_line_1: 'Main',
address_line_2: undefined,
address_line_3: undefined,
latitude: undefined,
longitude: undefined,
zip_code: undefined
},
relationships: {
localizable: {
data: {
type: 'mobile_users',
id: 452
}
}
}
};
t.context.locationJson = {
type: 'locations',
title: 'Location Title',
city: 'San Diego',
state: 'CA',
country: 'USA',
address_line_1: 'Main',
address_line_2: undefined,
address_line_3: undefined,
latitude: undefined,
longitude: undefined,
zip_code: undefined,
relationships: {
localizable: {
data: {
type: 'mobile_users',
id: 452
}
}
}
};
t.context.locationPatchJsonApi = {
id: 7,
type: 'locations',
attributes: {
title: 'Location Title',
address_line_1: 'Main',
city: 'Orange City',
state: 'CA',
country: 'USA'
}
};
client.storageUtility.clear();
t.context.storage = client.storageUtility;
client.location = new Location({});
t.context.location = new Location({ data: t.context.locationJsonApi });
});
/**
* @test {Location}
*/
test('[unit] Location should handle location data with a normal json format', (t) => {
const { locationJson } = t.context;
const location = new Location(locationJson);
t.is(location.type, 'locations');
t.is(location.title, 'Location Title');
t.is(location.addressLine1, 'Main');
t.is(location.city, 'San Diego');
t.is(location.state, 'CA');
t.is(location.country, 'USA');
t.is(location.relationships.relationships.localizable.data.type, 'mobile_users');
t.is(location.relationships.relationships.localizable.data.id, 452);
});
/**
* @test {Location}
*/
test('[unit] Location should handle location data with json api format', (t) => {
const { locationJsonApi } = t.context;
const location = new Location({ data: locationJsonApi });
t.is(location.type, 'locations');
t.is(location.title, 'Location Title');
t.is(location.addressLine1, 'Main');
t.is(location.city, 'San Diego');
t.is(location.state, 'CA');
t.is(location.country, 'USA');
t.is(location.relationships.relationships.localizable.data.type, 'mobile_users');
t.is(location.relationships.relationships.localizable.data.id, 452);
});
/**
* @test {Location}
*/
test('[unit] Location should generate json api format when converted to string', (t) => {
const { locationJsonApi } = t.context;
let location = new Location({ data: locationJsonApi });
t.deepEqual(location.toJSON(), locationJsonApi);
location = new Location({ data: locationJsonApi });
t.deepEqual(location.toJSON(), locationJsonApi);
});
/**
* @test {Location.save}
*/
test('[unit] Location.save should successfully save a new location when id does not exist', async (t) => {
const { locationJsonApi, insertResponse } = t.context;
const json = JSON.parse(JSON.stringify(locationJsonApi));
const location = new Location({ data: json });
let request = {};
nock(client.apiBaseUrl).post(`/v3/locations`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [201, insertResponse];
});
const response = await location.save();
t.is(request.path, `/v3/locations`);
t.is(request.headers.accept, 'application/json');
t.deepEqual(request.requestBody, { data: json });
t.is(request.headers['content-type'], 'application/json');
t.is(request.headers.authorization, 'Token token=valid_token');
t.is(response.id, 143);
t.is(response.type, 'locations');
t.is(response.country, 'USA');
t.is(response.state, 'CA');
t.is(response.city, 'San Diego');
t.is(response.latitude, null);
t.is(response.longitude, null);
t.is(response.title, 'Location Title');
t.is(response.zipCode, null);
t.is(response.addressLine1, null);
t.is(response.addressLine2, null);
t.is(response.addressLine3, null);
});
/**
* @test {Location.save}
*/
test('[unit] Location.save should successfully update a location with a location object when an id exists', async (t) => {
const { locationPatchJsonApi } = t.context;
let requestBody = {};
const server = nock(client.apiBaseUrl)
.patch('/v3/locations/7', (body) => {
requestBody = body;
return { data: json };
})
.reply(201, {
data: {
id: 7,
type: 'locations',
attributes: {
country: 'USA',
state: 'CA',
city: 'Orange City',
latitude: '32.8563846',
longitude: '-117.2029363',
title: 'Location Title',
zip_code: null,
address_line_1: 'Main',
address_line_2: null,
address_line_3: null
}
}
});
const json = JSON.parse(JSON.stringify(locationPatchJsonApi));
const location = new Location({ data: json });
const response = await location.save();
t.deepEqual(requestBody, { data: json });
server.done();
t.is(response.id, 7);
t.is(response.type, 'locations');
t.is(response.country, 'USA');
t.is(response.state, 'CA');
t.is(response.city, 'Orange City');
t.is(response.latitude, '32.8563846');
t.is(response.longitude, '-117.2029363');
t.is(response.title, 'Location Title');
t.is(response.zipCode, null);
t.is(response.addressLine1, 'Main');
t.is(response.addressLine2, null);
t.is(response.addressLine3, null);
});