@jesseditson/dnsimple
Version:
A Node.JS client for the DNSimple API.
217 lines (216 loc) • 11.7 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const nock = require("nock");
const main_1 = require("../lib/main");
const util_1 = require("./util");
const dnsimple = (0, util_1.createTestClient)();
describe("contacts", () => {
describe("#listContacts", () => {
const accountId = 1010;
it("supports pagination", () => __awaiter(void 0, void 0, void 0, function* () {
const scope = nock("https://api.dnsimple.com")
.get("/v2/1010/contacts?page=1")
.reply((0, util_1.readFixtureAt)("listContacts/success.http"));
yield dnsimple.contacts.listContacts(accountId, { page: 1 });
expect(scope.isDone()).toBeTruthy();
}));
it("supports extra request options", () => __awaiter(void 0, void 0, void 0, function* () {
const scope = nock("https://api.dnsimple.com")
.get("/v2/1010/contacts?foo=bar")
.reply((0, util_1.readFixtureAt)("listContacts/success.http"));
yield dnsimple.contacts.listContacts(accountId, { foo: "bar" });
expect(scope.isDone()).toBeTruthy();
}));
it("supports sorting", () => __awaiter(void 0, void 0, void 0, function* () {
const scope = nock("https://api.dnsimple.com")
.get("/v2/1010/contacts?sort=label%3Aasc")
.reply((0, util_1.readFixtureAt)("listContacts/success.http"));
yield dnsimple.contacts.listContacts(accountId, { sort: "label:asc" });
expect(scope.isDone()).toBeTruthy();
}));
it("supports filter", () => __awaiter(void 0, void 0, void 0, function* () {
const scope = nock("https://api.dnsimple.com")
.get("/v2/1010/contacts?first_name_like=example")
.reply((0, util_1.readFixtureAt)("listContacts/success.http"));
yield dnsimple.contacts.listContacts(accountId, {
first_name_like: "example",
});
expect(scope.isDone()).toBeTruthy();
}));
it("produces a contact list", () => __awaiter(void 0, void 0, void 0, function* () {
nock("https://api.dnsimple.com")
.get("/v2/1010/contacts")
.reply((0, util_1.readFixtureAt)("listContacts/success.http"));
const response = yield dnsimple.contacts.listContacts(accountId);
const contacts = response.data;
expect(contacts.length).toBe(2);
expect(contacts[0].account_id).toBe(1010);
expect(contacts[0].label).toBe("Default");
expect(contacts[0].first_name).toBe("First");
expect(contacts[0].last_name).toBe("User");
}));
it("exposes the pagination info", () => __awaiter(void 0, void 0, void 0, function* () {
nock("https://api.dnsimple.com")
.get("/v2/1010/contacts")
.reply((0, util_1.readFixtureAt)("listContacts/success.http"));
const response = yield dnsimple.contacts.listContacts(accountId);
const pagination = response.pagination;
expect(pagination).not.toBe(null);
expect(pagination.current_page).toBe(1);
}));
});
describe("#listContacts.collectAll", () => {
const accountId = 1010;
it("produces a complete list", () => __awaiter(void 0, void 0, void 0, function* () {
nock("https://api.dnsimple.com")
.get("/v2/1010/contacts?page=1")
.reply((0, util_1.readFixtureAt)("pages-1of3.http"));
nock("https://api.dnsimple.com")
.get("/v2/1010/contacts?page=2")
.reply((0, util_1.readFixtureAt)("pages-2of3.http"));
nock("https://api.dnsimple.com")
.get("/v2/1010/contacts?page=3")
.reply((0, util_1.readFixtureAt)("pages-3of3.http"));
const contacts = yield dnsimple.contacts.listContacts.collectAll(accountId);
expect(contacts.length).toBe(5);
expect(contacts[0].id).toBe(1);
expect(contacts[4].id).toBe(5);
}));
});
describe("#getContact", () => {
const accountId = 1010;
it("produces a contact", () => __awaiter(void 0, void 0, void 0, function* () {
nock("https://api.dnsimple.com")
.get("/v2/1010/contacts/1")
.reply((0, util_1.readFixtureAt)("getContact/success.http"));
const response = yield dnsimple.contacts.getContact(accountId, 1);
const contact = response.data;
expect(contact.id).toBe(1);
expect(contact.account_id).toBe(1010);
expect(contact.label).toBe("Default");
expect(contact.first_name).toBe("First");
expect(contact.last_name).toBe("User");
}));
describe("when the contact does not exist", () => {
it("produces an error", () => __awaiter(void 0, void 0, void 0, function* () {
nock("https://api.dnsimple.com")
.get("/v2/1010/contacts/0")
.reply((0, util_1.readFixtureAt)("notfound-contact.http"));
yield expect(dnsimple.contacts.getContact(accountId, 0)).rejects.toThrow(main_1.NotFoundError);
}));
});
});
describe("#createContact", () => {
const accountId = 1010;
const attributes = { first_name: "John", last_name: "Smith" };
it("builds the correct request", () => __awaiter(void 0, void 0, void 0, function* () {
const scope = nock("https://api.dnsimple.com")
.post("/v2/1010/contacts", attributes)
.reply((0, util_1.readFixtureAt)("createContact/created.http"));
yield dnsimple.contacts.createContact(accountId, attributes);
expect(scope.isDone()).toBeTruthy();
}));
it("produces a contact", () => __awaiter(void 0, void 0, void 0, function* () {
nock("https://api.dnsimple.com")
.post("/v2/1010/contacts", attributes)
.reply((0, util_1.readFixtureAt)("createContact/created.http"));
const response = yield dnsimple.contacts.createContact(accountId, attributes);
const contact = response.data;
expect(contact.id).toBe(1);
expect(contact.account_id).toBe(1010);
expect(contact.label).toBe("Default");
expect(contact.first_name).toBe("First");
expect(contact.last_name).toBe("User");
}));
it("includes validation errors coming from the API", () => __awaiter(void 0, void 0, void 0, function* () {
nock("https://api.dnsimple.com")
.post("/v2/1010/contacts", attributes)
.reply((0, util_1.readFixtureAt)("createContact/error-validation-errors.http"));
try {
yield dnsimple.contacts.createContact(accountId, attributes);
}
catch (error) {
expect(error.attributeErrors().address1).toEqual(["can't be blank"]);
expect(error.attributeErrors().city).toEqual(["can't be blank"]);
expect(error.attributeErrors().country).toEqual(["can't be blank"]);
expect(error.attributeErrors().email).toEqual([
"can't be blank",
"is an invalid email address",
]);
expect(error.attributeErrors().first_name).toEqual(["can't be blank"]);
expect(error.attributeErrors().last_name).toEqual(["can't be blank"]);
expect(error.attributeErrors().phone).toEqual([
"can't be blank",
"is probably not a phone number",
]);
expect(error.attributeErrors().postal_code).toEqual(["can't be blank"]);
expect(error.attributeErrors().state_province).toEqual([
"can't be blank",
]);
}
}));
});
describe("#updateContact", () => {
const accountId = 1010;
const contactId = 1;
const attributes = { last_name: "Buckminster" };
it("builds the correct request", () => __awaiter(void 0, void 0, void 0, function* () {
const scope = nock("https://api.dnsimple.com")
.patch("/v2/1010/contacts/" + contactId, attributes)
.reply((0, util_1.readFixtureAt)("updateContact/success.http"));
yield dnsimple.contacts.updateContact(accountId, contactId, attributes);
expect(scope.isDone()).toBeTruthy();
}));
it("produces a contact", () => __awaiter(void 0, void 0, void 0, function* () {
nock("https://api.dnsimple.com")
.patch("/v2/1010/contacts/" + contactId, attributes)
.reply((0, util_1.readFixtureAt)("updateContact/success.http"));
const response = yield dnsimple.contacts.updateContact(accountId, contactId, attributes);
const contact = response.data;
expect(contact.id).toBe(1);
}));
describe("when the contact does not exist", () => {
it("produces an error", () => __awaiter(void 0, void 0, void 0, function* () {
nock("https://api.dnsimple.com")
.get("/v2/1010/contacts/0", attributes)
.reply((0, util_1.readFixtureAt)("notfound-contact.http"));
yield expect(dnsimple.contacts.updateContact(accountId, 0, attributes)).rejects.toThrow();
}));
});
});
describe("#deleteContact", () => {
const accountId = 1010;
const contactId = 1;
it("builds the correct request", () => __awaiter(void 0, void 0, void 0, function* () {
const scope = nock("https://api.dnsimple.com")
.delete("/v2/1010/contacts/" + contactId)
.reply((0, util_1.readFixtureAt)("deleteContact/success.http"));
yield dnsimple.contacts.deleteContact(accountId, contactId);
expect(scope.isDone()).toBeTruthy();
}));
it("produces nothing", () => __awaiter(void 0, void 0, void 0, function* () {
nock("https://api.dnsimple.com")
.delete("/v2/1010/contacts/" + contactId)
.reply((0, util_1.readFixtureAt)("deleteContact/success.http"));
const response = yield dnsimple.contacts.deleteContact(accountId, contactId);
expect(response).toEqual({});
}));
describe("when the contact does not exist", () => {
it("produces an error", () => __awaiter(void 0, void 0, void 0, function* () {
nock("https://api.dnsimple.com")
.delete("/v2/1010/contacts/0")
.reply((0, util_1.readFixtureAt)("notfound-contact.http"));
yield expect(dnsimple.contacts.deleteContact(accountId, 0)).rejects.toThrow(main_1.NotFoundError);
}));
});
});
});
;