Skip to content

Commit

Permalink
GH-33: Added call resource reference tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jirenius committed Nov 22, 2019
1 parent b4966ae commit ef4cee0
Showing 1 changed file with 168 additions and 51 deletions.
219 changes: 168 additions & 51 deletions src/class/ResClient.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ describe("ResClient", () => {
'service.primitives': primitiveCollectionData
}
};
const modelReferenceResources = {
rid: "service.model.ref",
models: {
"service.model.ref": modelData
}
};

function flushPromises(depth = 2) {
return new Promise(resolve => setImmediate(() => {
Expand Down Expand Up @@ -1269,84 +1275,106 @@ describe("ResClient", () => {

describe("ResModel", () => {

it("calls remote method on call with parameters", () => {
return getServerResource('service.model', modelResources).then(model => {
model.call('test', { zoo: "baz", value: 12 });
describe("call", () => {

return flushRequests().then(() => {
expect(server.pendingRequests()).toBe(1);
expect(server.error).toBe(null);
let req = server.getNextRequest();
expect(req.method).toBe('call.service.model.test');
expect(req.params).toEqual({ zoo: "baz", value: 12 });
it("calls remote method on call with parameters", () => {
return getServerResource('service.model', modelResources).then(model => {
model.call('test', { zoo: "baz", value: 12 });

return flushRequests().then(() => {
expect(server.pendingRequests()).toBe(1);
expect(server.error).toBe(null);
let req = server.getNextRequest();
expect(req.method).toBe('call.service.model.test');
expect(req.params).toEqual({ zoo: "baz", value: 12 });
});
});
});
});

it("calls remote method on call without parameters", () => {
return getServerResource('service.model', modelResources).then(model => {
model.call('test');
return flushRequests().then(() => {
let req = server.getNextRequest();
expect([ null, undefined ]).toContain(req.params);
it("calls remote method on call without parameters", () => {
return getServerResource('service.model', modelResources).then(model => {
model.call('test');
return flushRequests().then(() => {
let req = server.getNextRequest();
expect([ null, undefined ]).toContain(req.params);
});
});
});
});

it("calls set method on set", () => {
return getServerResource('service.model', modelResources).then(model => {
model.set({ foo: "baz" });
return flushRequests().then(() => {
let req = server.getNextRequest();
expect(req.method).toBe('call.service.model.set');
expect(req.params).toEqual({ foo: "baz" });
it("resolves call promise on result payload", () => {
return getServerResource('service.model', modelResources).then(model => {
let promise = model.call('test');

return flushRequests().then(() => {
let req = server.getNextRequest();
server.sendResponse(req, { payload: { responseValue: true }});

return flushRequests().then(() => {
return expect(promise).resolves.toEqual({ responseValue: true });
});
});
});
});
});

it("resolves call promise on success", () => {
return getServerResource('service.model', modelResources).then(model => {
let promise = model.call('test');

return flushRequests().then(() => {
let req = server.getNextRequest();
server.sendResponse(req, { payload: { responseValue: true }});
it("resolves call promise on success with legacy resgate", () => {
return getServerResource('service.model', modelResources, null, null).then(model => {
let promise = model.call('test');

return flushRequests().then(() => {
return expect(promise).resolves.toEqual({ responseValue: true });
let req = server.getNextRequest();
server.sendResponse(req, { responseValue: true });

return flushRequests().then(() => {
return expect(promise).resolves.toEqual({ responseValue: true });
});
});
});
});
});

it("resolves call promise on success with legacy resgate", () => {
return getServerResource('service.model', modelResources, null, null).then(model => {
let promise = model.call('test');
it("rejects call promise on error", () => {
return getServerResource('service.model', modelResources).then(model => {
let promise = model.call('test');

return flushRequests().then(() => {
let req = server.getNextRequest();
server.sendResponse(req, { responseValue: true });
return flushRequests().then(() => {
let req = server.getNextRequest();
expect(req).not.toBe(undefined);
server.sendError(req, 'service.testError', "Test error");

return expect(promise).rejects.toEqual(expect.objectContaining({
code: 'service.testError',
message: "Test error"
}));
});
});
});

it("resolves call promise on resource response", () => {
return getServerResource('service.model', modelResources).then(model => {
let promise = model.call('test');

return flushRequests().then(() => {
return expect(promise).resolves.toEqual({ responseValue: true });
let req = server.getNextRequest();
server.sendResponse(req, modelReferenceResources);

return flushRequests()
.then(() => promise)
.then(m => {
expect(m).not.toBe(undefined);
expect(m.foo).toBe("bar");
});
});
});
});

});

it("rejects call promise on error", () => {
it("calls set method on set", () => {
return getServerResource('service.model', modelResources).then(model => {
let promise = model.call('test');

model.set({ foo: "baz" });
return flushRequests().then(() => {
let req = server.getNextRequest();
expect(req).not.toBe(undefined);
server.sendError(req, 'service.testError', "Test error");

return expect(promise).rejects.toEqual(expect.objectContaining({
code: 'service.testError',
message: "Test error"
}));
expect(req.method).toBe('call.service.model.set');
expect(req.params).toEqual({ foo: "baz" });
});
});
});
Expand Down Expand Up @@ -1476,6 +1504,95 @@ describe("ResClient", () => {
]);
});
});

it("calls remote method on call with parameters", () => {
return getServerResource('service.collection', collectionResources).then(collection => {
collection.call('test', { zoo: "baz", value: 12 });

return flushRequests().then(() => {
expect(server.pendingRequests()).toBe(1);
expect(server.error).toBe(null);
let req = server.getNextRequest();
expect(req.method).toBe('call.service.collection.test');
expect(req.params).toEqual({ zoo: "baz", value: 12 });
});
});
});

it("calls remote method on call without parameters", () => {
return getServerResource('service.collection', collectionResources).then(collection => {
collection.call('test');
return flushRequests().then(() => {
let req = server.getNextRequest();
expect([ null, undefined ]).toContain(req.params);
});
});
});

it("resolves call promise on success", () => {
return getServerResource('service.collection', collectionResources).then(collection => {
let promise = collection.call('test');

return flushRequests().then(() => {
let req = server.getNextRequest();
server.sendResponse(req, { payload: { responseValue: true }});

return flushRequests().then(() => {
return expect(promise).resolves.toEqual({ responseValue: true });
});
});
});
});

it("resolves call promise on success with legacy resgate", () => {
return getServerResource('service.collection', collectionResources, null, null).then(collection => {
let promise = collection.call('test');

return flushRequests().then(() => {
let req = server.getNextRequest();
server.sendResponse(req, { responseValue: true });

return flushRequests().then(() => {
return expect(promise).resolves.toEqual({ responseValue: true });
});
});
});
});

it("rejects call promise on error", () => {
return getServerResource('service.collection', collectionResources).then(collection => {
let promise = collection.call('test');

return flushRequests().then(() => {
let req = server.getNextRequest();
expect(req).not.toBe(undefined);
server.sendError(req, 'service.testError', "Test error");

return expect(promise).rejects.toEqual(expect.objectContaining({
code: 'service.testError',
message: "Test error"
}));
});
});
});

it("resolves call promise on resource response", () => {
return getServerResource('service.collection', collectionResources).then(collection => {
let promise = collection.call('test');

return flushRequests().then(() => {
let req = server.getNextRequest();
server.sendResponse(req, modelReferenceResources);

return flushRequests()
.then(() => promise)
.then(m => {
expect(m).not.toBe(undefined);
expect(m.foo).toBe("bar");
});
});
});
});
});

});

0 comments on commit ef4cee0

Please sign in to comment.