This commit is contained in:
Sebastian 2023-01-03 10:01:04 -03:00
parent a2668c22f0
commit 9ad1f2712a
No known key found for this signature in database
GPG Key ID: BE4FF68352439FC1
10 changed files with 659 additions and 471 deletions

View File

@ -29,14 +29,17 @@ import {
} from "../hooks/instance.js"; } from "../hooks/instance.js";
import { expect } from "chai"; import { expect } from "chai";
import { ApiMockEnvironment } from "../hooks/testing.js"; import { ApiMockEnvironment } from "../hooks/testing.js";
import { API_CREATE_INSTANCE, API_UPDATE_CURRENT_INSTANCE_AUTH, API_UPDATE_INSTANCE_AUTH_BY_ID } from "../hooks/urls.js"; import {
API_CREATE_INSTANCE,
API_UPDATE_CURRENT_INSTANCE_AUTH,
API_UPDATE_INSTANCE_AUTH_BY_ID,
} from "../hooks/urls.js";
interface TestingContextProps { interface TestingContextProps {
children?: ComponentChildren; children?: ComponentChildren;
} }
describe("backend context api ", () => { describe("backend context api ", () => {
it("should use new token after updating the instance token in the settings as user", async () => { it("should use new token after updating the instance token in the settings as user", async () => {
const env = new ApiMockEnvironment(); const env = new ApiMockEnvironment();
@ -61,10 +64,12 @@ describe("backend context api ", () => {
} as MerchantBackend.Instances.QueryInstancesResponse, } as MerchantBackend.Instances.QueryInstancesResponse,
}); });
management.setNewToken("another_token") management.setNewToken("another_token");
}, },
({ instance, management, admin }) => { ({ instance, management, admin }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
env.addRequestExpectation(API_CREATE_INSTANCE, { env.addRequestExpectation(API_CREATE_INSTANCE, {
auth: "another_token", auth: "another_token",
@ -76,9 +81,10 @@ describe("backend context api ", () => {
admin.createInstance({ admin.createInstance({
id: "new_instance_id", id: "new_instance_id",
} as MerchantBackend.Instances.InstanceConfigurationMessage); } as MerchantBackend.Instances.InstanceConfigurationMessage);
}, },
], env.buildTestingContext()); ],
env.buildTestingContext(),
);
expect(hookBehavior).deep.eq({ result: "ok" }); expect(hookBehavior).deep.eq({ result: "ok" });
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" });
@ -110,7 +116,9 @@ describe("backend context api ", () => {
instance.setNewToken("another_token"); instance.setNewToken("another_token");
}, },
({ instance, management, admin }) => { ({ instance, management, admin }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
env.addRequestExpectation(API_CREATE_INSTANCE, { env.addRequestExpectation(API_CREATE_INSTANCE, {
auth: "another_token", auth: "another_token",
@ -123,7 +131,9 @@ describe("backend context api ", () => {
id: "new_instance_id", id: "new_instance_id",
} as MerchantBackend.Instances.InstanceConfigurationMessage); } as MerchantBackend.Instances.InstanceConfigurationMessage);
}, },
], env.buildTestingContext()); ],
env.buildTestingContext(),
);
expect(hookBehavior).deep.eq({ result: "ok" }); expect(hookBehavior).deep.eq({ result: "ok" });
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" });

View File

@ -22,7 +22,13 @@
import { tests } from "@gnu-taler/web-util/lib/index.browser"; import { tests } from "@gnu-taler/web-util/lib/index.browser";
import { expect } from "chai"; import { expect } from "chai";
import { MerchantBackend } from "../declaration.js"; import { MerchantBackend } from "../declaration.js";
import { useAdminAPI, useBackendInstances, useInstanceAPI, useInstanceDetails, useManagementAPI } from "./instance.js"; import {
useAdminAPI,
useBackendInstances,
useInstanceAPI,
useInstanceDetails,
useManagementAPI,
} from "./instance.js";
import { ApiMockEnvironment } from "./testing.js"; import { ApiMockEnvironment } from "./testing.js";
import { import {
API_CREATE_INSTANCE, API_CREATE_INSTANCE,
@ -30,17 +36,17 @@ import {
API_GET_CURRENT_INSTANCE, API_GET_CURRENT_INSTANCE,
API_LIST_INSTANCES, API_LIST_INSTANCES,
API_UPDATE_CURRENT_INSTANCE, API_UPDATE_CURRENT_INSTANCE,
API_UPDATE_CURRENT_INSTANCE_AUTH, API_UPDATE_INSTANCE_BY_ID API_UPDATE_CURRENT_INSTANCE_AUTH,
API_UPDATE_INSTANCE_BY_ID,
} from "./urls.js"; } from "./urls.js";
describe("instance api interaction with details", () => { describe("instance api interaction with details", () => {
it("should evict cache when updating an instance", async () => { it("should evict cache when updating an instance", async () => {
const env = new ApiMockEnvironment(); const env = new ApiMockEnvironment();
env.addRequestExpectation(API_GET_CURRENT_INSTANCE, { env.addRequestExpectation(API_GET_CURRENT_INSTANCE, {
response: { response: {
name: 'instance_name' name: "instance_name",
} as MerchantBackend.Instances.QueryInstancesResponse, } as MerchantBackend.Instances.QueryInstancesResponse,
}); });
@ -56,42 +62,47 @@ describe("instance api interaction with details", () => {
expect(query.loading).true; expect(query.loading).true;
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).false; expect(query.loading).false;
expect(query.ok).true expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
expect(query.data).deep.equals({ expect(query.data).deep.equals({
name: 'instance_name' name: "instance_name",
}); });
env.addRequestExpectation(API_UPDATE_CURRENT_INSTANCE, { env.addRequestExpectation(API_UPDATE_CURRENT_INSTANCE, {
request: { request: {
name: 'other_name' name: "other_name",
} as MerchantBackend.Instances.InstanceReconfigurationMessage, } as MerchantBackend.Instances.InstanceReconfigurationMessage,
}); });
env.addRequestExpectation(API_GET_CURRENT_INSTANCE, { env.addRequestExpectation(API_GET_CURRENT_INSTANCE, {
response: { response: {
name: 'other_name' name: "other_name",
} as MerchantBackend.Instances.QueryInstancesResponse, } as MerchantBackend.Instances.QueryInstancesResponse,
}); });
api.updateInstance({ api.updateInstance({
name: 'other_name' name: "other_name",
} as MerchantBackend.Instances.InstanceReconfigurationMessage); } as MerchantBackend.Instances.InstanceReconfigurationMessage);
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).false; expect(query.loading).false;
expect(query.ok).true expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
expect(query.data).deep.equals({ expect(query.data).deep.equals({
name: 'other_name' name: "other_name",
}); });
}, },
], env.buildTestingContext()); ],
env.buildTestingContext(),
);
expect(hookBehavior).deep.eq({ result: "ok" }); expect(hookBehavior).deep.eq({ result: "ok" });
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" });
}); });
it("should evict cache when setting the instance's token", async () => { it("should evict cache when setting the instance's token", async () => {
@ -99,11 +110,11 @@ describe("instance api interaction with details", () => {
env.addRequestExpectation(API_GET_CURRENT_INSTANCE, { env.addRequestExpectation(API_GET_CURRENT_INSTANCE, {
response: { response: {
name: 'instance_name', name: "instance_name",
auth: { auth: {
method: 'token', method: "token",
token: 'not-secret', token: "not-secret",
} },
} as MerchantBackend.Instances.QueryInstancesResponse, } as MerchantBackend.Instances.QueryInstancesResponse,
}); });
@ -119,51 +130,56 @@ describe("instance api interaction with details", () => {
expect(query.loading).true; expect(query.loading).true;
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).false; expect(query.loading).false;
expect(query.ok).true expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
expect(query.data).deep.equals({ expect(query.data).deep.equals({
name: 'instance_name', name: "instance_name",
auth: { auth: {
method: 'token', method: "token",
token: 'not-secret', token: "not-secret",
} },
}); });
env.addRequestExpectation(API_UPDATE_CURRENT_INSTANCE_AUTH, { env.addRequestExpectation(API_UPDATE_CURRENT_INSTANCE_AUTH, {
request: { request: {
method: 'token', method: "token",
token: 'secret' token: "secret",
} as MerchantBackend.Instances.InstanceAuthConfigurationMessage, } as MerchantBackend.Instances.InstanceAuthConfigurationMessage,
}); });
env.addRequestExpectation(API_GET_CURRENT_INSTANCE, { env.addRequestExpectation(API_GET_CURRENT_INSTANCE, {
response: { response: {
name: 'instance_name', name: "instance_name",
auth: { auth: {
method: 'token', method: "token",
token: 'secret', token: "secret",
} },
} as MerchantBackend.Instances.QueryInstancesResponse, } as MerchantBackend.Instances.QueryInstancesResponse,
}); });
api.setNewToken('secret') api.setNewToken("secret");
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).false; expect(query.loading).false;
expect(query.ok).true expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
expect(query.data).deep.equals({ expect(query.data).deep.equals({
name: 'instance_name', name: "instance_name",
auth: { auth: {
method: 'token', method: "token",
token: 'secret', token: "secret",
} },
}); });
}, },
], env.buildTestingContext()); ],
env.buildTestingContext(),
);
expect(hookBehavior).deep.eq({ result: "ok" }); expect(hookBehavior).deep.eq({ result: "ok" });
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" });
}); });
it("should evict cache when clearing the instance's token", async () => { it("should evict cache when clearing the instance's token", async () => {
@ -171,11 +187,11 @@ describe("instance api interaction with details", () => {
env.addRequestExpectation(API_GET_CURRENT_INSTANCE, { env.addRequestExpectation(API_GET_CURRENT_INSTANCE, {
response: { response: {
name: 'instance_name', name: "instance_name",
auth: { auth: {
method: 'token', method: "token",
token: 'not-secret', token: "not-secret",
} },
} as MerchantBackend.Instances.QueryInstancesResponse, } as MerchantBackend.Instances.QueryInstancesResponse,
}); });
@ -191,46 +207,52 @@ describe("instance api interaction with details", () => {
expect(query.loading).true; expect(query.loading).true;
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).false; expect(query.loading).false;
expect(query.ok).true expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
expect(query.data).deep.equals({ expect(query.data).deep.equals({
name: 'instance_name', name: "instance_name",
auth: { auth: {
method: 'token', method: "token",
token: 'not-secret', token: "not-secret",
} },
}); });
env.addRequestExpectation(API_UPDATE_CURRENT_INSTANCE_AUTH, { env.addRequestExpectation(API_UPDATE_CURRENT_INSTANCE_AUTH, {
request: { request: {
method: 'external', method: "external",
} as MerchantBackend.Instances.InstanceAuthConfigurationMessage, } as MerchantBackend.Instances.InstanceAuthConfigurationMessage,
}); });
env.addRequestExpectation(API_GET_CURRENT_INSTANCE, { env.addRequestExpectation(API_GET_CURRENT_INSTANCE, {
response: { response: {
name: 'instance_name', name: "instance_name",
auth: { auth: {
method: 'external', method: "external",
} },
} as MerchantBackend.Instances.QueryInstancesResponse, } as MerchantBackend.Instances.QueryInstancesResponse,
}); });
api.clearToken(); api.clearToken();
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).false; expect(query.loading).false;
expect(query.ok).true expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
expect(query.data).deep.equals({ expect(query.data).deep.equals({
name: 'instance_name', name: "instance_name",
auth: { auth: {
method: 'external', method: "external",
} },
}); });
}, },
], env.buildTestingContext()); ],
env.buildTestingContext(),
);
expect(hookBehavior).deep.eq({ result: "ok" }); expect(hookBehavior).deep.eq({ result: "ok" });
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" });
// const { result, waitForNextUpdate } = renderHook( // const { result, waitForNextUpdate } = renderHook(
@ -266,14 +288,12 @@ describe("instance api interaction with details", () => {
// } // }
// }); // });
// act(async () => { // act(async () => {
// await result.current?.api.clearToken(); // await result.current?.api.clearToken();
// }); // });
// expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); // expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" });
// expect(result.current.query.loading).false; // expect(result.current.query.loading).false;
// await waitForNextUpdate({ timeout: 1 }); // await waitForNextUpdate({ timeout: 1 });
@ -293,15 +313,16 @@ describe("instance api interaction with details", () => {
}); });
describe("instance admin api interaction with listing", () => { describe("instance admin api interaction with listing", () => {
it("should evict cache when creating a new instance", async () => { it("should evict cache when creating a new instance", async () => {
const env = new ApiMockEnvironment(); const env = new ApiMockEnvironment();
env.addRequestExpectation(API_LIST_INSTANCES, { env.addRequestExpectation(API_LIST_INSTANCES, {
response: { response: {
instances: [{ instances: [
name: 'instance_name' {
} as MerchantBackend.Instances.Instance] name: "instance_name",
} as MerchantBackend.Instances.Instance,
],
}, },
}); });
@ -317,53 +338,65 @@ describe("instance admin api interaction with listing", () => {
expect(query.loading).true; expect(query.loading).true;
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).false; expect(query.loading).false;
expect(query.ok).true expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
expect(query.data).deep.equals({ expect(query.data).deep.equals({
instances: [{ instances: [
name: 'instance_name' {
}] name: "instance_name",
},
],
}); });
env.addRequestExpectation(API_CREATE_INSTANCE, { env.addRequestExpectation(API_CREATE_INSTANCE, {
request: { request: {
name: 'other_name' name: "other_name",
} as MerchantBackend.Instances.InstanceConfigurationMessage, } as MerchantBackend.Instances.InstanceConfigurationMessage,
}); });
env.addRequestExpectation(API_LIST_INSTANCES, { env.addRequestExpectation(API_LIST_INSTANCES, {
response: { response: {
instances: [{ instances: [
name: 'instance_name' {
} as MerchantBackend.Instances.Instance, name: "instance_name",
{ } as MerchantBackend.Instances.Instance,
name: 'other_name' {
} as MerchantBackend.Instances.Instance] name: "other_name",
} as MerchantBackend.Instances.Instance,
],
}, },
}); });
api.createInstance({ api.createInstance({
name: 'other_name' name: "other_name",
} as MerchantBackend.Instances.InstanceConfigurationMessage); } as MerchantBackend.Instances.InstanceConfigurationMessage);
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).false; expect(query.loading).false;
expect(query.ok).true expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
expect(query.data).deep.equals({ expect(query.data).deep.equals({
instances: [{ instances: [
name: 'instance_name' {
}, { name: "instance_name",
name: 'other_name' },
}] {
name: "other_name",
},
],
}); });
}, },
], env.buildTestingContext()); ],
env.buildTestingContext(),
);
expect(hookBehavior).deep.eq({ result: "ok" }); expect(hookBehavior).deep.eq({ result: "ok" });
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" });
}); });
it("should evict cache when deleting an instance", async () => { it("should evict cache when deleting an instance", async () => {
@ -371,14 +404,16 @@ describe("instance admin api interaction with listing", () => {
env.addRequestExpectation(API_LIST_INSTANCES, { env.addRequestExpectation(API_LIST_INSTANCES, {
response: { response: {
instances: [{ instances: [
id: 'default', {
name: 'instance_name' id: "default",
} as MerchantBackend.Instances.Instance, name: "instance_name",
{ } as MerchantBackend.Instances.Instance,
id: 'the_id', {
name: 'second_instance' id: "the_id",
} as MerchantBackend.Instances.Instance] name: "second_instance",
} as MerchantBackend.Instances.Instance,
],
}, },
}); });
@ -394,45 +429,58 @@ describe("instance admin api interaction with listing", () => {
expect(query.loading).true; expect(query.loading).true;
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).false; expect(query.loading).false;
expect(query.ok).true expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
expect(query.data).deep.equals({ expect(query.data).deep.equals({
instances: [{ instances: [
id: 'default', {
name: 'instance_name' id: "default",
}, { name: "instance_name",
id: 'the_id', },
name: 'second_instance' {
}] id: "the_id",
name: "second_instance",
},
],
}); });
env.addRequestExpectation(API_DELETE_INSTANCE('the_id'), {}); env.addRequestExpectation(API_DELETE_INSTANCE("the_id"), {});
env.addRequestExpectation(API_LIST_INSTANCES, { env.addRequestExpectation(API_LIST_INSTANCES, {
response: { response: {
instances: [{ instances: [
id: 'default', {
name: 'instance_name' id: "default",
} as MerchantBackend.Instances.Instance] name: "instance_name",
} as MerchantBackend.Instances.Instance,
],
}, },
}); });
api.deleteInstance('the_id'); api.deleteInstance("the_id");
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).false; expect(query.loading).false;
expect(query.ok).true expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
expect(query.data).deep.equals({ expect(query.data).deep.equals({
instances: [{ instances: [
id: 'default', {
name: 'instance_name' id: "default",
}] name: "instance_name",
},
],
}); });
}, },
], env.buildTestingContext()); ],
env.buildTestingContext(),
);
expect(hookBehavior).deep.eq({ result: "ok" }); expect(hookBehavior).deep.eq({ result: "ok" });
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" });
@ -510,14 +558,16 @@ describe("instance admin api interaction with listing", () => {
env.addRequestExpectation(API_LIST_INSTANCES, { env.addRequestExpectation(API_LIST_INSTANCES, {
response: { response: {
instances: [{ instances: [
id: 'default', {
name: 'instance_name' id: "default",
} as MerchantBackend.Instances.Instance, name: "instance_name",
{ } as MerchantBackend.Instances.Instance,
id: 'the_id', {
name: 'second_instance' id: "the_id",
} as MerchantBackend.Instances.Instance] name: "second_instance",
} as MerchantBackend.Instances.Instance,
],
}, },
}); });
@ -533,72 +583,85 @@ describe("instance admin api interaction with listing", () => {
expect(query.loading).true; expect(query.loading).true;
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).false; expect(query.loading).false;
expect(query.ok).true expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
expect(query.data).deep.equals({ expect(query.data).deep.equals({
instances: [{ instances: [
id: 'default', {
name: 'instance_name' id: "default",
}, { name: "instance_name",
id: 'the_id', },
name: 'second_instance' {
}] id: "the_id",
name: "second_instance",
},
],
}); });
env.addRequestExpectation(API_DELETE_INSTANCE('the_id'), { env.addRequestExpectation(API_DELETE_INSTANCE("the_id"), {
qparam: { qparam: {
purge: 'YES' purge: "YES",
} },
}); });
env.addRequestExpectation(API_LIST_INSTANCES, { env.addRequestExpectation(API_LIST_INSTANCES, {
response: { response: {
instances: [{ instances: [
id: 'default', {
name: 'instance_name' id: "default",
} as MerchantBackend.Instances.Instance] name: "instance_name",
} as MerchantBackend.Instances.Instance,
],
}, },
}); });
api.purgeInstance('the_id') api.purgeInstance("the_id");
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).false; expect(query.loading).false;
expect(query.ok).true expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
expect(query.data).deep.equals({ expect(query.data).deep.equals({
instances: [{ instances: [
id: 'default', {
name: 'instance_name' id: "default",
}] name: "instance_name",
},
],
}); });
}, },
], env.buildTestingContext()); ],
env.buildTestingContext(),
);
expect(hookBehavior).deep.eq({ result: "ok" }); expect(hookBehavior).deep.eq({ result: "ok" });
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" });
}); });
}); });
describe("instance management api interaction with listing", () => { describe("instance management api interaction with listing", () => {
it("should evict cache when updating an instance", async () => { it("should evict cache when updating an instance", async () => {
const env = new ApiMockEnvironment(); const env = new ApiMockEnvironment();
env.addRequestExpectation(API_LIST_INSTANCES, { env.addRequestExpectation(API_LIST_INSTANCES, {
response: { response: {
instances: [{ instances: [
id: 'managed', {
name: 'instance_name' id: "managed",
} as MerchantBackend.Instances.Instance] name: "instance_name",
} as MerchantBackend.Instances.Instance,
],
}, },
}); });
const hookBehavior = await tests.hookBehaveLikeThis( const hookBehavior = await tests.hookBehaveLikeThis(
() => { () => {
const api = useManagementAPI('managed'); const api = useManagementAPI("managed");
const query = useBackendInstances(); const query = useBackendInstances();
return { query, api }; return { query, api };
}, },
@ -608,53 +671,61 @@ describe("instance management api interaction with listing", () => {
expect(query.loading).true; expect(query.loading).true;
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).false; expect(query.loading).false;
expect(query.ok).true expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
expect(query.data).deep.equals({ expect(query.data).deep.equals({
instances: [{ instances: [
id: 'managed', {
name: 'instance_name' id: "managed",
}] name: "instance_name",
},
],
}); });
env.addRequestExpectation(API_UPDATE_INSTANCE_BY_ID('managed'), { env.addRequestExpectation(API_UPDATE_INSTANCE_BY_ID("managed"), {
request: { request: {
name: 'other_name' name: "other_name",
} as MerchantBackend.Instances.InstanceReconfigurationMessage, } as MerchantBackend.Instances.InstanceReconfigurationMessage,
}); });
env.addRequestExpectation(API_LIST_INSTANCES, { env.addRequestExpectation(API_LIST_INSTANCES, {
response: { response: {
instances: [ instances: [
{ {
id: 'managed', id: "managed",
name: 'other_name' name: "other_name",
} as MerchantBackend.Instances.Instance] } as MerchantBackend.Instances.Instance,
],
}, },
}); });
api.updateInstance({ api.updateInstance({
name: 'other_name' name: "other_name",
} as MerchantBackend.Instances.InstanceConfigurationMessage); } as MerchantBackend.Instances.InstanceConfigurationMessage);
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).false; expect(query.loading).false;
expect(query.ok).true expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
expect(query.data).deep.equals({ expect(query.data).deep.equals({
instances: [{ instances: [
id: 'managed', {
name: 'other_name' id: "managed",
}] name: "other_name",
},
],
}); });
}, },
], env.buildTestingContext()); ],
env.buildTestingContext(),
);
expect(hookBehavior).deep.eq({ result: "ok" }); expect(hookBehavior).deep.eq({ result: "ok" });
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" });
}); });
}); });

View File

@ -29,11 +29,11 @@ import {
API_DELETE_ORDER, API_DELETE_ORDER,
API_FORGET_ORDER_BY_ID, API_FORGET_ORDER_BY_ID,
API_GET_ORDER_BY_ID, API_GET_ORDER_BY_ID,
API_LIST_ORDERS, API_REFUND_ORDER_BY_ID API_LIST_ORDERS,
API_REFUND_ORDER_BY_ID,
} from "./urls.js"; } from "./urls.js";
describe("order api interaction with listing", () => { describe("order api interaction with listing", () => {
it("should evict cache when creating an order", async () => { it("should evict cache when creating an order", async () => {
const env = new ApiMockEnvironment(); const env = new ApiMockEnvironment();
@ -51,7 +51,6 @@ describe("order api interaction with listing", () => {
}, },
}); });
const newDate = (d: Date) => { const newDate = (d: Date) => {
//console.log("new date", d); //console.log("new date", d);
}; };
@ -68,9 +67,11 @@ describe("order api interaction with listing", () => {
expect(query.loading).true; expect(query.loading).true;
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).undefined; expect(query.loading).undefined;
expect(query.ok).true expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
expect(query.data).deep.equals({ expect(query.data).deep.equals({
orders: [{ order_id: "1" }, { order_id: "2" }], orders: [{ order_id: "1" }, { order_id: "2" }],
@ -102,19 +103,22 @@ describe("order api interaction with listing", () => {
} as any); } as any);
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).undefined; expect(query.loading).undefined;
expect(query.ok).true expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
expect(query.data).deep.equals({ expect(query.data).deep.equals({
orders: [{ order_id: "1" }, { order_id: "2" }, { order_id: "3" }], orders: [{ order_id: "1" }, { order_id: "2" }, { order_id: "3" }],
}); });
}, },
], env.buildTestingContext()); ],
env.buildTestingContext(),
);
expect(hookBehavior).deep.eq({ result: "ok" }); expect(hookBehavior).deep.eq({ result: "ok" });
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" });
}); });
it("should evict cache when doing a refund", async () => { it("should evict cache when doing a refund", async () => {
@ -123,16 +127,21 @@ describe("order api interaction with listing", () => {
env.addRequestExpectation(API_LIST_ORDERS, { env.addRequestExpectation(API_LIST_ORDERS, {
qparam: { delta: 0, paid: "yes" }, qparam: { delta: 0, paid: "yes" },
response: { response: {
orders: [{ order_id: "1", amount: 'EUR:12', refundable: true } as MerchantBackend.Orders.OrderHistoryEntry], orders: [
{
order_id: "1",
amount: "EUR:12",
refundable: true,
} as MerchantBackend.Orders.OrderHistoryEntry,
],
}, },
}); });
env.addRequestExpectation(API_LIST_ORDERS, { env.addRequestExpectation(API_LIST_ORDERS, {
qparam: { delta: -20, paid: "yes" }, qparam: { delta: -20, paid: "yes" },
response: { orders: [], }, response: { orders: [] },
}); });
const newDate = (d: Date) => { const newDate = (d: Date) => {
//console.log("new date", d); //console.log("new date", d);
}; };
@ -149,61 +158,72 @@ describe("order api interaction with listing", () => {
expect(query.loading).true; expect(query.loading).true;
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).undefined; expect(query.loading).undefined;
expect(query.ok).true expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
expect(query.data).deep.equals({ expect(query.data).deep.equals({
orders: [{ orders: [
order_id: "1", {
amount: 'EUR:12', order_id: "1",
refundable: true, amount: "EUR:12",
}], refundable: true,
},
],
}); });
env.addRequestExpectation(API_REFUND_ORDER_BY_ID('1'), { env.addRequestExpectation(API_REFUND_ORDER_BY_ID("1"), {
request: { request: {
reason: 'double pay', reason: "double pay",
refund: 'EUR:1' refund: "EUR:1",
}, },
}); });
env.addRequestExpectation(API_LIST_ORDERS, { env.addRequestExpectation(API_LIST_ORDERS, {
qparam: { delta: 0, paid: "yes" }, qparam: { delta: 0, paid: "yes" },
response: { response: {
orders: [{ order_id: "1", amount: 'EUR:12', refundable: false } as any], orders: [
{ order_id: "1", amount: "EUR:12", refundable: false } as any,
],
}, },
}); });
env.addRequestExpectation(API_LIST_ORDERS, { env.addRequestExpectation(API_LIST_ORDERS, {
qparam: { delta: -20, paid: "yes" }, qparam: { delta: -20, paid: "yes" },
response: { orders: [], }, response: { orders: [] },
}); });
api.refundOrder('1', { api.refundOrder("1", {
reason: 'double pay', reason: "double pay",
refund: 'EUR:1' refund: "EUR:1",
}); });
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).undefined; expect(query.loading).undefined;
expect(query.ok).true expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
expect(query.data).deep.equals({ expect(query.data).deep.equals({
orders: [{ orders: [
order_id: "1", {
amount: 'EUR:12', order_id: "1",
refundable: false, amount: "EUR:12",
}], refundable: false,
},
],
}); });
}, },
], env.buildTestingContext()); ],
env.buildTestingContext(),
);
expect(hookBehavior).deep.eq({ result: "ok" }); expect(hookBehavior).deep.eq({ result: "ok" });
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" });
}); });
it("should evict cache when deleting an order", async () => { it("should evict cache when deleting an order", async () => {
@ -223,7 +243,6 @@ describe("order api interaction with listing", () => {
}, },
}); });
const newDate = (d: Date) => { const newDate = (d: Date) => {
//console.log("new date", d); //console.log("new date", d);
}; };
@ -240,15 +259,17 @@ describe("order api interaction with listing", () => {
expect(query.loading).true; expect(query.loading).true;
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).undefined; expect(query.loading).undefined;
expect(query.ok).true expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
expect(query.data).deep.equals({ expect(query.data).deep.equals({
orders: [{ order_id: "1" }, { order_id: "2" }], orders: [{ order_id: "1" }, { order_id: "2" }],
}); });
env.addRequestExpectation(API_DELETE_ORDER('1'), {}); env.addRequestExpectation(API_DELETE_ORDER("1"), {});
env.addRequestExpectation(API_LIST_ORDERS, { env.addRequestExpectation(API_LIST_ORDERS, {
qparam: { delta: 0, paid: "yes" }, qparam: { delta: 0, paid: "yes" },
@ -264,35 +285,36 @@ describe("order api interaction with listing", () => {
}, },
}); });
api.deleteOrder('1'); api.deleteOrder("1");
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).undefined; expect(query.loading).undefined;
expect(query.ok).true expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
expect(query.data).deep.equals({ expect(query.data).deep.equals({
orders: [{ order_id: "2" }], orders: [{ order_id: "2" }],
}); });
}, },
], env.buildTestingContext()); ],
env.buildTestingContext(),
);
expect(hookBehavior).deep.eq({ result: "ok" }); expect(hookBehavior).deep.eq({ result: "ok" });
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" });
}); });
}); });
describe("order api interaction with details", () => { describe("order api interaction with details", () => {
it("should evict cache when doing a refund", async () => { it("should evict cache when doing a refund", async () => {
const env = new ApiMockEnvironment(); const env = new ApiMockEnvironment();
env.addRequestExpectation(API_GET_ORDER_BY_ID('1'), { env.addRequestExpectation(API_GET_ORDER_BY_ID("1"), {
// qparam: { delta: 0, paid: "yes" }, // qparam: { delta: 0, paid: "yes" },
response: { response: {
summary: 'description', summary: "description",
refund_amount: 'EUR:0', refund_amount: "EUR:0",
} as unknown as MerchantBackend.Orders.CheckPaymentPaidResponse, } as unknown as MerchantBackend.Orders.CheckPaymentPaidResponse,
}); });
@ -302,7 +324,7 @@ describe("order api interaction with details", () => {
const hookBehavior = await tests.hookBehaveLikeThis( const hookBehavior = await tests.hookBehaveLikeThis(
() => { () => {
const query = useOrderDetails('1') const query = useOrderDetails("1");
const api = useOrderAPI(); const api = useOrderAPI();
return { query, api }; return { query, api };
}, },
@ -312,58 +334,63 @@ describe("order api interaction with details", () => {
expect(query.loading).true; expect(query.loading).true;
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).false; expect(query.loading).false;
expect(query.ok).true expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
expect(query.data).deep.equals({ expect(query.data).deep.equals({
summary: 'description', summary: "description",
refund_amount: 'EUR:0', refund_amount: "EUR:0",
}); });
env.addRequestExpectation(API_REFUND_ORDER_BY_ID('1'), { env.addRequestExpectation(API_REFUND_ORDER_BY_ID("1"), {
request: { request: {
reason: 'double pay', reason: "double pay",
refund: 'EUR:1' refund: "EUR:1",
}, },
}); });
env.addRequestExpectation(API_GET_ORDER_BY_ID('1'), { env.addRequestExpectation(API_GET_ORDER_BY_ID("1"), {
response: { response: {
summary: 'description', summary: "description",
refund_amount: 'EUR:1', refund_amount: "EUR:1",
} as unknown as MerchantBackend.Orders.CheckPaymentPaidResponse, } as unknown as MerchantBackend.Orders.CheckPaymentPaidResponse,
}); });
api.refundOrder('1', { api.refundOrder("1", {
reason: 'double pay', reason: "double pay",
refund: 'EUR:1' refund: "EUR:1",
})
},
({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" });
expect(query.loading).false;
expect(query.ok).true
if (!query.ok) return;
expect(query.data).deep.equals({
summary: 'description',
refund_amount: 'EUR:1',
}); });
}, },
], env.buildTestingContext()); ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).false;
expect(query.ok).true;
if (!query.ok) return;
expect(query.data).deep.equals({
summary: "description",
refund_amount: "EUR:1",
});
},
],
env.buildTestingContext(),
);
expect(hookBehavior).deep.eq({ result: "ok" }); expect(hookBehavior).deep.eq({ result: "ok" });
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" });
});
})
it("should evict cache when doing a forget", async () => { it("should evict cache when doing a forget", async () => {
const env = new ApiMockEnvironment(); const env = new ApiMockEnvironment();
env.addRequestExpectation(API_GET_ORDER_BY_ID('1'), { env.addRequestExpectation(API_GET_ORDER_BY_ID("1"), {
// qparam: { delta: 0, paid: "yes" }, // qparam: { delta: 0, paid: "yes" },
response: { response: {
summary: 'description', summary: "description",
refund_amount: 'EUR:0', refund_amount: "EUR:0",
} as unknown as MerchantBackend.Orders.CheckPaymentPaidResponse, } as unknown as MerchantBackend.Orders.CheckPaymentPaidResponse,
}); });
@ -373,7 +400,7 @@ describe("order api interaction with details", () => {
const hookBehavior = await tests.hookBehaveLikeThis( const hookBehavior = await tests.hookBehaveLikeThis(
() => { () => {
const query = useOrderDetails('1') const query = useOrderDetails("1");
const api = useOrderAPI(); const api = useOrderAPI();
return { query, api }; return { query, api };
}, },
@ -383,48 +410,52 @@ describe("order api interaction with details", () => {
expect(query.loading).true; expect(query.loading).true;
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).false; expect(query.loading).false;
expect(query.ok).true expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
expect(query.data).deep.equals({ expect(query.data).deep.equals({
summary: 'description', summary: "description",
refund_amount: 'EUR:0', refund_amount: "EUR:0",
}); });
env.addRequestExpectation(API_FORGET_ORDER_BY_ID('1'), { env.addRequestExpectation(API_FORGET_ORDER_BY_ID("1"), {
request: { request: {
fields: ['$.summary'] fields: ["$.summary"],
}, },
}); });
env.addRequestExpectation(API_GET_ORDER_BY_ID('1'), { env.addRequestExpectation(API_GET_ORDER_BY_ID("1"), {
response: { response: {
summary: undefined, summary: undefined,
} as unknown as MerchantBackend.Orders.CheckPaymentPaidResponse, } as unknown as MerchantBackend.Orders.CheckPaymentPaidResponse,
}); });
api.forgetOrder('1', { api.forgetOrder("1", {
fields: ['$.summary'] fields: ["$.summary"],
}) });
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).false; expect(query.loading).false;
expect(query.ok).true expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
expect(query.data).deep.equals({ expect(query.data).deep.equals({
summary: undefined, summary: undefined,
}); });
}, },
], env.buildTestingContext()); ],
env.buildTestingContext(),
);
expect(hookBehavior).deep.eq({ result: "ok" }); expect(hookBehavior).deep.eq({ result: "ok" });
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" });
});
}) });
})
describe("order listing pagination", () => { describe("order listing pagination", () => {
it("should not load more if has reach the end", async () => { it("should not load more if has reach the end", async () => {
const env = new ApiMockEnvironment(); const env = new ApiMockEnvironment();
env.addRequestExpectation(API_LIST_ORDERS, { env.addRequestExpectation(API_LIST_ORDERS, {
@ -441,7 +472,6 @@ describe("order listing pagination", () => {
}, },
}); });
const newDate = (d: Date) => { const newDate = (d: Date) => {
//console.log("new date", d); //console.log("new date", d);
}; };
@ -449,7 +479,7 @@ describe("order listing pagination", () => {
const hookBehavior = await tests.hookBehaveLikeThis( const hookBehavior = await tests.hookBehaveLikeThis(
() => { () => {
const date = new Date(12); const date = new Date(12);
const query = useInstanceOrders({ wired: "yes", date }, newDate) const query = useInstanceOrders({ wired: "yes", date }, newDate);
const api = useOrderAPI(); const api = useOrderAPI();
return { query, api }; return { query, api };
}, },
@ -459,33 +489,40 @@ describe("order listing pagination", () => {
expect(query.loading).true; expect(query.loading).true;
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).undefined; expect(query.loading).undefined;
expect(query.ok).true expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
expect(query.data).deep.equals({ expect(query.data).deep.equals({
orders: [{ order_id: "1" }, { order_id: "2" }], orders: [{ order_id: "1" }, { order_id: "2" }],
}); });
expect(query.isReachingEnd).true expect(query.isReachingEnd).true;
expect(query.isReachingStart).true expect(query.isReachingStart).true;
// should not trigger new state update or query // should not trigger new state update or query
query.loadMore() query.loadMore();
query.loadMorePrev(); query.loadMorePrev();
}, },
], env.buildTestingContext()); ],
env.buildTestingContext(),
);
expect(hookBehavior).deep.eq({ result: "ok" }); expect(hookBehavior).deep.eq({ result: "ok" });
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" });
}); });
it("should load more if result brings more that PAGE_SIZE", async () => { it("should load more if result brings more that PAGE_SIZE", async () => {
const env = new ApiMockEnvironment(); const env = new ApiMockEnvironment();
const ordersFrom0to20 = Array.from({ length: 20 }).map((e, i) => ({ order_id: String(i) })) const ordersFrom0to20 = Array.from({ length: 20 }).map((e, i) => ({
const ordersFrom20to40 = Array.from({ length: 20 }).map((e, i) => ({ order_id: String(i + 20) })) order_id: String(i),
const ordersFrom20to0 = [...ordersFrom0to20].reverse() }));
const ordersFrom20to40 = Array.from({ length: 20 }).map((e, i) => ({
order_id: String(i + 20),
}));
const ordersFrom20to0 = [...ordersFrom0to20].reverse();
env.addRequestExpectation(API_LIST_ORDERS, { env.addRequestExpectation(API_LIST_ORDERS, {
qparam: { delta: 20, wired: "yes", date_ms: 12 }, qparam: { delta: 20, wired: "yes", date_ms: 12 },
@ -508,7 +545,7 @@ describe("order listing pagination", () => {
const hookBehavior = await tests.hookBehaveLikeThis( const hookBehavior = await tests.hookBehaveLikeThis(
() => { () => {
const date = new Date(12); const date = new Date(12);
const query = useInstanceOrders({ wired: "yes", date }, newDate) const query = useInstanceOrders({ wired: "yes", date }, newDate);
const api = useOrderAPI(); const api = useOrderAPI();
return { query, api }; return { query, api };
}, },
@ -518,62 +555,77 @@ describe("order listing pagination", () => {
expect(query.loading).true; expect(query.loading).true;
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).undefined; expect(query.loading).undefined;
expect(query.ok).true expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
expect(query.data).deep.equals({ expect(query.data).deep.equals({
orders: [...ordersFrom20to0, ...ordersFrom20to40], orders: [...ordersFrom20to0, ...ordersFrom20to40],
}); });
expect(query.isReachingEnd).false expect(query.isReachingEnd).false;
expect(query.isReachingStart).false expect(query.isReachingStart).false;
env.addRequestExpectation(API_LIST_ORDERS, { env.addRequestExpectation(API_LIST_ORDERS, {
qparam: { delta: -40, wired: "yes", date_ms: 13 }, qparam: { delta: -40, wired: "yes", date_ms: 13 },
response: { response: {
orders: [...ordersFrom20to40, { order_id: '41' }], orders: [...ordersFrom20to40, { order_id: "41" }],
}, },
}); });
query.loadMore() query.loadMore();
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).true; expect(query.loading).true;
}, },
({ query, api }) => { ({ query, api }) => {
expect(query.loading).undefined; expect(query.loading).undefined;
expect(query.ok).true expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
expect(query.data).deep.equals({ expect(query.data).deep.equals({
orders: [...ordersFrom20to0, ...ordersFrom20to40, { order_id: '41' }], orders: [
...ordersFrom20to0,
...ordersFrom20to40,
{ order_id: "41" },
],
}); });
env.addRequestExpectation(API_LIST_ORDERS, { env.addRequestExpectation(API_LIST_ORDERS, {
qparam: { delta: 40, wired: "yes", date_ms: 12 }, qparam: { delta: 40, wired: "yes", date_ms: 12 },
response: { response: {
orders: [...ordersFrom0to20, { order_id: '-1' }], orders: [...ordersFrom0to20, { order_id: "-1" }],
}, },
}); });
query.loadMorePrev() query.loadMorePrev();
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).true; expect(query.loading).true;
}, },
({ query, api }) => { ({ query, api }) => {
expect(query.loading).undefined; expect(query.loading).undefined;
expect(query.ok).true expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
expect(query.data).deep.equals({ expect(query.data).deep.equals({
orders: [{ order_id: '-1' }, ...ordersFrom20to0, ...ordersFrom20to40, { order_id: '41' }], orders: [
{ order_id: "-1" },
...ordersFrom20to0,
...ordersFrom20to40,
{ order_id: "41" },
],
}); });
}, },
], env.buildTestingContext()); ],
env.buildTestingContext(),
);
expect(hookBehavior).deep.eq({ result: "ok" }); expect(hookBehavior).deep.eq({ result: "ok" });
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" });
}); });
}); });

View File

@ -257,9 +257,9 @@ export function useInstanceOrders(
!beforeData || !afterData !beforeData || !afterData
? [] ? []
: (beforeData || lastBefore).data.orders : (beforeData || lastBefore).data.orders
.slice() .slice()
.reverse() .reverse()
.concat((afterData || lastAfter).data.orders); .concat((afterData || lastAfter).data.orders);
if (loadingAfter || loadingBefore) return { loading: true, data: { orders } }; if (loadingAfter || loadingBefore) return { loading: true, data: { orders } };
if (beforeData && afterData) { if (beforeData && afterData) {
return { ok: true, data: { orders }, ...pagination }; return { ok: true, data: { orders }, ...pagination };

View File

@ -22,13 +22,18 @@
import { tests } from "@gnu-taler/web-util/lib/index.browser"; import { tests } from "@gnu-taler/web-util/lib/index.browser";
import { expect } from "chai"; import { expect } from "chai";
import { MerchantBackend } from "../declaration.js"; import { MerchantBackend } from "../declaration.js";
import { useInstanceProducts, useProductAPI, useProductDetails } from "./product.js"; import {
useInstanceProducts,
useProductAPI,
useProductDetails,
} from "./product.js";
import { ApiMockEnvironment } from "./testing.js"; import { ApiMockEnvironment } from "./testing.js";
import { import {
API_CREATE_PRODUCT, API_CREATE_PRODUCT,
API_DELETE_PRODUCT, API_GET_PRODUCT_BY_ID, API_DELETE_PRODUCT,
API_GET_PRODUCT_BY_ID,
API_LIST_PRODUCTS, API_LIST_PRODUCTS,
API_UPDATE_PRODUCT_BY_ID API_UPDATE_PRODUCT_BY_ID,
} from "./urls.js"; } from "./urls.js";
describe("product api interaction with listing", () => { describe("product api interaction with listing", () => {
@ -56,19 +61,21 @@ describe("product api interaction with listing", () => {
expect(query.loading).true; expect(query.loading).true;
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).true; expect(query.loading).true;
}, },
({ query, api }) => { ({ query, api }) => {
expect(query.loading).undefined; expect(query.loading).undefined;
expect(query.ok).true expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
expect(query.data).deep.equals([ expect(query.data).deep.equals([{ id: "1234", price: "ARS:12" }]);
{ id: "1234", price: "ARS:12" },
]);
env.addRequestExpectation(API_CREATE_PRODUCT, { env.addRequestExpectation(API_CREATE_PRODUCT, {
request: { price: "ARS:23" } as MerchantBackend.Products.ProductAddDetail, request: {
price: "ARS:23",
} as MerchantBackend.Products.ProductAddDetail,
}); });
env.addRequestExpectation(API_LIST_PRODUCTS, { env.addRequestExpectation(API_LIST_PRODUCTS, {
@ -77,28 +84,37 @@ describe("product api interaction with listing", () => {
}, },
}); });
env.addRequestExpectation(API_GET_PRODUCT_BY_ID("1234"), { env.addRequestExpectation(API_GET_PRODUCT_BY_ID("1234"), {
response: { price: "ARS:12" } as MerchantBackend.Products.ProductDetail, response: {
price: "ARS:12",
} as MerchantBackend.Products.ProductDetail,
}); });
env.addRequestExpectation(API_GET_PRODUCT_BY_ID("1234"), { env.addRequestExpectation(API_GET_PRODUCT_BY_ID("1234"), {
response: { price: "ARS:12" } as MerchantBackend.Products.ProductDetail, response: {
price: "ARS:12",
} as MerchantBackend.Products.ProductDetail,
}); });
env.addRequestExpectation(API_GET_PRODUCT_BY_ID("2345"), { env.addRequestExpectation(API_GET_PRODUCT_BY_ID("2345"), {
response: { price: "ARS:23" } as MerchantBackend.Products.ProductDetail, response: {
price: "ARS:23",
} as MerchantBackend.Products.ProductDetail,
}); });
api.createProduct({ api.createProduct({
price: "ARS:23", price: "ARS:23",
} as any) } as any);
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).true; expect(query.loading).true;
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).undefined; expect(query.loading).undefined;
expect(query.ok).true expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
expect(query.data).deep.equals([ expect(query.data).deep.equals([
{ {
@ -111,11 +127,12 @@ describe("product api interaction with listing", () => {
}, },
]); ]);
}, },
], env.buildTestingContext()); ],
env.buildTestingContext(),
);
expect(hookBehavior).deep.eq({ result: "ok" }); expect(hookBehavior).deep.eq({ result: "ok" });
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" });
}); });
it("should evict cache when updating a product", async () => { it("should evict cache when updating a product", async () => {
@ -145,16 +162,18 @@ describe("product api interaction with listing", () => {
expect(query.loading).true; expect(query.loading).true;
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).undefined; expect(query.loading).undefined;
expect(query.ok).true expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
expect(query.data).deep.equals([ expect(query.data).deep.equals([{ id: "1234", price: "ARS:12" }]);
{ id: "1234", price: "ARS:12" },
]);
env.addRequestExpectation(API_UPDATE_PRODUCT_BY_ID("1234"), { env.addRequestExpectation(API_UPDATE_PRODUCT_BY_ID("1234"), {
request: { price: "ARS:13" } as MerchantBackend.Products.ProductPatchDetail, request: {
price: "ARS:13",
} as MerchantBackend.Products.ProductPatchDetail,
}); });
env.addRequestExpectation(API_LIST_PRODUCTS, { env.addRequestExpectation(API_LIST_PRODUCTS, {
@ -163,18 +182,21 @@ describe("product api interaction with listing", () => {
}, },
}); });
env.addRequestExpectation(API_GET_PRODUCT_BY_ID("1234"), { env.addRequestExpectation(API_GET_PRODUCT_BY_ID("1234"), {
response: { price: "ARS:13" } as MerchantBackend.Products.ProductDetail, response: {
price: "ARS:13",
} as MerchantBackend.Products.ProductDetail,
}); });
api.updateProduct("1234", { api.updateProduct("1234", {
price: "ARS:13", price: "ARS:13",
} as any) } as any);
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).undefined; expect(query.loading).undefined;
expect(query.ok).true expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
expect(query.data).deep.equals([ expect(query.data).deep.equals([
{ {
@ -183,11 +205,12 @@ describe("product api interaction with listing", () => {
}, },
]); ]);
}, },
], env.buildTestingContext()); ],
env.buildTestingContext(),
);
expect(hookBehavior).deep.eq({ result: "ok" }); expect(hookBehavior).deep.eq({ result: "ok" });
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" });
}); });
it("should evict cache when deleting a product", async () => { it("should evict cache when deleting a product", async () => {
@ -217,12 +240,14 @@ describe("product api interaction with listing", () => {
expect(query.loading).true; expect(query.loading).true;
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).true; expect(query.loading).true;
}, },
({ query, api }) => { ({ query, api }) => {
expect(query.loading).undefined; expect(query.loading).undefined;
expect(query.ok).true expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
expect(query.data).deep.equals([ expect(query.data).deep.equals([
{ id: "1234", price: "ARS:12" }, { id: "1234", price: "ARS:12" },
@ -238,29 +263,34 @@ describe("product api interaction with listing", () => {
}); });
env.addRequestExpectation(API_GET_PRODUCT_BY_ID("1234"), { env.addRequestExpectation(API_GET_PRODUCT_BY_ID("1234"), {
response: { price: "ARS:12" } as MerchantBackend.Products.ProductDetail, response: {
price: "ARS:12",
} as MerchantBackend.Products.ProductDetail,
}); });
api.deleteProduct("2345"); api.deleteProduct("2345");
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).true; expect(query.loading).true;
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).undefined; expect(query.loading).undefined;
expect(query.ok).true expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
expect(query.data).deep.equals([ expect(query.data).deep.equals([{ id: "1234", price: "ARS:12" }]);
{ id: "1234", price: "ARS:12" },
]);
}, },
], env.buildTestingContext()); ],
env.buildTestingContext(),
);
expect(hookBehavior).deep.eq({ result: "ok" }); expect(hookBehavior).deep.eq({ result: "ok" });
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" });
}); });
}); });
describe("product api interaction with details", () => { describe("product api interaction with details", () => {
@ -285,16 +315,20 @@ describe("product api interaction with details", () => {
expect(query.loading).true; expect(query.loading).true;
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).false; expect(query.loading).false;
expect(query.ok).true expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
expect(query.data).deep.equals({ expect(query.data).deep.equals({
description: "this is a description", description: "this is a description",
}); });
env.addRequestExpectation(API_UPDATE_PRODUCT_BY_ID("12"), { env.addRequestExpectation(API_UPDATE_PRODUCT_BY_ID("12"), {
request: { description: "other description" } as MerchantBackend.Products.ProductPatchDetail, request: {
description: "other description",
} as MerchantBackend.Products.ProductPatchDetail,
}); });
env.addRequestExpectation(API_GET_PRODUCT_BY_ID("12"), { env.addRequestExpectation(API_GET_PRODUCT_BY_ID("12"), {
@ -306,21 +340,23 @@ describe("product api interaction with details", () => {
api.updateProduct("12", { api.updateProduct("12", {
description: "other description", description: "other description",
} as any); } as any);
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).false; expect(query.loading).false;
expect(query.ok).true expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
expect(query.data).deep.equals({ expect(query.data).deep.equals({
description: "other description", description: "other description",
}); });
}, },
], env.buildTestingContext()); ],
env.buildTestingContext(),
);
expect(hookBehavior).deep.eq({ result: "ok" }); expect(hookBehavior).deep.eq({ result: "ok" });
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" });
});
}) });
})

View File

@ -25,7 +25,7 @@ import {
useInstanceReserves, useInstanceReserves,
useReserveDetails, useReserveDetails,
useReservesAPI, useReservesAPI,
useTipDetails useTipDetails,
} from "./reserves.js"; } from "./reserves.js";
import { ApiMockEnvironment } from "./testing.js"; import { ApiMockEnvironment } from "./testing.js";
import { import {
@ -35,7 +35,7 @@ import {
API_DELETE_RESERVE, API_DELETE_RESERVE,
API_GET_RESERVE_BY_ID, API_GET_RESERVE_BY_ID,
API_GET_TIP_BY_ID, API_GET_TIP_BY_ID,
API_LIST_RESERVES API_LIST_RESERVES,
} from "./urls.js"; } from "./urls.js";
import { tests } from "@gnu-taler/web-util/lib/index.browser"; import { tests } from "@gnu-taler/web-util/lib/index.browser";
@ -66,7 +66,7 @@ describe("reserve api interaction with listing", () => {
}, },
({ query, api }) => { ({ query, api }) => {
expect(query.loading).false; expect(query.loading).false;
expect(query.ok).true expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
expect(query.data).deep.equals({ expect(query.data).deep.equals({
reserves: [{ reserve_pub: "11" }], reserves: [{ reserve_pub: "11" }],
@ -101,10 +101,12 @@ describe("reserve api interaction with listing", () => {
initial_balance: "ARS:3333", initial_balance: "ARS:3333",
exchange_url: "http://url", exchange_url: "http://url",
wire_method: "iban", wire_method: "iban",
}) });
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).false; expect(query.loading).false;
expect(query.ok).true; expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
@ -120,11 +122,12 @@ describe("reserve api interaction with listing", () => {
], ],
}); });
}, },
], env.buildTestingContext()); ],
env.buildTestingContext(),
);
expect(hookBehavior).deep.eq({ result: "ok" }); expect(hookBehavior).deep.eq({ result: "ok" });
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" });
}); });
it("should evict cache when deleting a reserve", async () => { it("should evict cache when deleting a reserve", async () => {
@ -158,10 +161,12 @@ describe("reserve api interaction with listing", () => {
expect(query.loading).true; expect(query.loading).true;
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }) expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).false; expect(query.loading).false;
expect(query.ok).true expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
expect(query.data).deep.equals({ expect(query.data).deep.equals({
reserves: [ reserves: [
@ -185,25 +190,25 @@ describe("reserve api interaction with listing", () => {
}, },
}); });
api.deleteReserve("11") api.deleteReserve("11");
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }) expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).false; expect(query.loading).false;
expect(query.ok).true expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
expect(query.data).deep.equals({ expect(query.data).deep.equals({
reserves: [ reserves: [{ reserve_pub: "22" }, { reserve_pub: "33" }],
{ reserve_pub: "22" },
{ reserve_pub: "33" },
],
}); });
}, },
], env.buildTestingContext()); ],
env.buildTestingContext(),
);
expect(hookBehavior).deep.eq({ result: "ok" }); expect(hookBehavior).deep.eq({ result: "ok" });
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" });
}); });
}); });
@ -217,8 +222,8 @@ describe("reserve api interaction with details", () => {
tips: [{ reason: "why?", tip_id: "id1", total_amount: "USD:10" }], tips: [{ reason: "why?", tip_id: "id1", total_amount: "USD:10" }],
} as MerchantBackend.Tips.ReserveDetail, } as MerchantBackend.Tips.ReserveDetail,
qparam: { qparam: {
tips: "yes" tips: "yes",
} },
}); });
const hookBehavior = await tests.hookBehaveLikeThis( const hookBehavior = await tests.hookBehaveLikeThis(
@ -233,9 +238,11 @@ describe("reserve api interaction with details", () => {
expect(query.loading).true; expect(query.loading).true;
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).false; expect(query.loading).false;
expect(query.ok).true expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
expect(query.data).deep.equals({ expect(query.data).deep.equals({
payto_uri: "payto://here", payto_uri: "payto://here",
@ -253,8 +260,8 @@ describe("reserve api interaction with details", () => {
taler_tip_uri: "uri", taler_tip_uri: "uri",
tip_expiration: { t_s: 1 }, tip_expiration: { t_s: 1 },
tip_status_url: "url", tip_status_url: "url",
} },
},); });
env.addRequestExpectation(API_GET_RESERVE_BY_ID("11"), { env.addRequestExpectation(API_GET_RESERVE_BY_ID("11"), {
response: { response: {
@ -265,18 +272,20 @@ describe("reserve api interaction with details", () => {
], ],
} as MerchantBackend.Tips.ReserveDetail, } as MerchantBackend.Tips.ReserveDetail,
qparam: { qparam: {
tips: "yes" tips: "yes",
} },
}); });
api.authorizeTipReserve("11", { api.authorizeTipReserve("11", {
amount: "USD:12", amount: "USD:12",
justification: "not", justification: "not",
next_url: "http://taler.net", next_url: "http://taler.net",
}) });
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).false; expect(query.loading).false;
expect(query.loading).false; expect(query.loading).false;
@ -291,11 +300,12 @@ describe("reserve api interaction with details", () => {
], ],
}); });
}, },
], env.buildTestingContext()); ],
env.buildTestingContext(),
);
expect(hookBehavior).deep.eq({ result: "ok" }); expect(hookBehavior).deep.eq({ result: "ok" });
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" });
}); });
it("should evict cache when adding a tip for a random reserve", async () => { it("should evict cache when adding a tip for a random reserve", async () => {
@ -307,8 +317,8 @@ describe("reserve api interaction with details", () => {
tips: [{ reason: "why?", tip_id: "id1", total_amount: "USD:10" }], tips: [{ reason: "why?", tip_id: "id1", total_amount: "USD:10" }],
} as MerchantBackend.Tips.ReserveDetail, } as MerchantBackend.Tips.ReserveDetail,
qparam: { qparam: {
tips: "yes" tips: "yes",
} },
}); });
const hookBehavior = await tests.hookBehaveLikeThis( const hookBehavior = await tests.hookBehaveLikeThis(
@ -323,9 +333,11 @@ describe("reserve api interaction with details", () => {
expect(query.loading).true; expect(query.loading).true;
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).false; expect(query.loading).false;
expect(query.ok).true expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
expect(query.data).deep.equals({ expect(query.data).deep.equals({
payto_uri: "payto://here", payto_uri: "payto://here",
@ -355,8 +367,8 @@ describe("reserve api interaction with details", () => {
], ],
} as MerchantBackend.Tips.ReserveDetail, } as MerchantBackend.Tips.ReserveDetail,
qparam: { qparam: {
tips: "yes" tips: "yes",
} },
}); });
api.authorizeTip({ api.authorizeTip({
@ -364,10 +376,11 @@ describe("reserve api interaction with details", () => {
justification: "not", justification: "not",
next_url: "http://taler.net", next_url: "http://taler.net",
}); });
}, },
({ query, api }) => { ({ query, api }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).false; expect(query.loading).false;
expect(query.ok).true; expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
@ -380,7 +393,9 @@ describe("reserve api interaction with details", () => {
], ],
}); });
}, },
], env.buildTestingContext()); ],
env.buildTestingContext(),
);
expect(hookBehavior).deep.eq({ result: "ok" }); expect(hookBehavior).deep.eq({ result: "ok" });
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" });
@ -388,7 +403,6 @@ describe("reserve api interaction with details", () => {
}); });
describe("reserve api interaction with tip details", () => { describe("reserve api interaction with tip details", () => {
it("should list tips", async () => { it("should list tips", async () => {
const env = new ApiMockEnvironment(); const env = new ApiMockEnvironment();
@ -398,8 +412,8 @@ describe("reserve api interaction with tip details", () => {
reason: "not", reason: "not",
} as MerchantBackend.Tips.TipDetails, } as MerchantBackend.Tips.TipDetails,
qparam: { qparam: {
pickups: "yes" pickups: "yes",
} },
}); });
const hookBehavior = await tests.hookBehaveLikeThis( const hookBehavior = await tests.hookBehaveLikeThis(
@ -410,22 +424,25 @@ describe("reserve api interaction with tip details", () => {
{}, {},
[ [
({ query }) => { ({ query }) => {
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({
result: "ok",
});
expect(query.loading).true; expect(query.loading).true;
}, },
({ query }) => { ({ query }) => {
expect(query.loading).false; expect(query.loading).false;
expect(query.ok).true expect(query.ok).true;
if (!query.ok) return; if (!query.ok) return;
expect(query.data).deep.equals({ expect(query.data).deep.equals({
total_picked_up: "USD:12", total_picked_up: "USD:12",
reason: "not", reason: "not",
}); });
}, },
], env.buildTestingContext()); ],
env.buildTestingContext(),
);
expect(hookBehavior).deep.eq({ result: "ok" }); expect(hookBehavior).deep.eq({ result: "ok" });
expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" }); expect(env.assertJustExpectedRequestWereMade()).deep.eq({ result: "ok" });
}); });
}); });

View File

@ -174,9 +174,10 @@ export function useInstanceTemplates(
if (afterData.data.templates.length < MAX_RESULT_SIZE) { if (afterData.data.templates.length < MAX_RESULT_SIZE) {
setPageAfter(pageAfter + 1); setPageAfter(pageAfter + 1);
} else { } else {
const from = `${afterData.data.templates[afterData.data.templates.length - 1] const from = `${
.template_id afterData.data.templates[afterData.data.templates.length - 1]
}`; .template_id
}`;
if (from && updatePosition) updatePosition(from); if (from && updatePosition) updatePosition(from);
} }
}, },

View File

@ -141,9 +141,10 @@ export function useInstanceTransfers(
if (afterData.data.transfers.length < MAX_RESULT_SIZE) { if (afterData.data.transfers.length < MAX_RESULT_SIZE) {
setPageAfter(pageAfter + 1); setPageAfter(pageAfter + 1);
} else { } else {
const from = `${afterData.data.transfers[afterData.data.transfers.length - 1] const from = `${
afterData.data.transfers[afterData.data.transfers.length - 1]
.transfer_serial_id .transfer_serial_id
}`; }`;
if (from && updatePosition) updatePosition(from); if (from && updatePosition) updatePosition(from);
} }
}, },
@ -152,9 +153,10 @@ export function useInstanceTransfers(
if (beforeData.data.transfers.length < MAX_RESULT_SIZE) { if (beforeData.data.transfers.length < MAX_RESULT_SIZE) {
setPageBefore(pageBefore + 1); setPageBefore(pageBefore + 1);
} else if (beforeData) { } else if (beforeData) {
const from = `${beforeData.data.transfers[beforeData.data.transfers.length - 1] const from = `${
beforeData.data.transfers[beforeData.data.transfers.length - 1]
.transfer_serial_id .transfer_serial_id
}`; }`;
if (from && updatePosition) updatePosition(from); if (from && updatePosition) updatePosition(from);
} }
}, },
@ -164,9 +166,9 @@ export function useInstanceTransfers(
!beforeData || !afterData !beforeData || !afterData
? [] ? []
: (beforeData || lastBefore).data.transfers : (beforeData || lastBefore).data.transfers
.slice() .slice()
.reverse() .reverse()
.concat((afterData || lastAfter).data.transfers); .concat((afterData || lastAfter).data.transfers);
if (loadingAfter || loadingBefore) if (loadingAfter || loadingBefore)
return { loading: true, data: { transfers } }; return { loading: true, data: { transfers } };
if (beforeData && afterData) { if (beforeData && afterData) {

View File

@ -15,74 +15,73 @@
*/ */
/** /**
* *
* @author Sebastian Javier Marchano (sebasjm) * @author Sebastian Javier Marchano (sebasjm)
*/ */
import { expect } from "chai"; import { expect } from "chai";
import { AMOUNT_REGEX, PAYTO_REGEX } from "../../src/utils/constants.js"; import { AMOUNT_REGEX, PAYTO_REGEX } from "../../src/utils/constants.js";
describe('payto uri format', () => { describe("payto uri format", () => {
const valids = [ const valids = [
'payto://iban/DE75512108001245126199?amount=EUR:200.0&message=hello', "payto://iban/DE75512108001245126199?amount=EUR:200.0&message=hello",
'payto://ach/122000661/1234', "payto://ach/122000661/1234",
'payto://upi/alice@example.com?receiver-name=Alice&amount=INR:200', "payto://upi/alice@example.com?receiver-name=Alice&amount=INR:200",
'payto://void/?amount=EUR:10.5', "payto://void/?amount=EUR:10.5",
'payto://ilp/g.acme.bob' "payto://ilp/g.acme.bob",
] ];
it('should be valid', () => { it("should be valid", () => {
valids.forEach(v => expect(v).match(PAYTO_REGEX)) valids.forEach((v) => expect(v).match(PAYTO_REGEX));
}); });
const invalids = [ const invalids = [
// has two question marks // has two question marks
'payto://iban/DE75?512108001245126199?amount=EUR:200.0&message=hello', "payto://iban/DE75?512108001245126199?amount=EUR:200.0&message=hello",
// has a space // has a space
'payto://ach /122000661/1234', "payto://ach /122000661/1234",
// has a space // has a space
'payto://upi/alice@ example.com?receiver-name=Alice&amount=INR:200', "payto://upi/alice@ example.com?receiver-name=Alice&amount=INR:200",
// invalid field name (mount instead of amount) // invalid field name (mount instead of amount)
'payto://void/?mount=EUR:10.5', "payto://void/?mount=EUR:10.5",
// payto:// is incomplete // payto:// is incomplete
'payto: //ilp/g.acme.bob' "payto: //ilp/g.acme.bob",
] ];
it('should not be valid', () => { it("should not be valid", () => {
invalids.forEach(v => expect(v).not.match(PAYTO_REGEX)) invalids.forEach((v) => expect(v).not.match(PAYTO_REGEX));
}); });
}) });
describe('amount format', () => { describe("amount format", () => {
const valids = [ const valids = [
'ARS:10', "ARS:10",
'COL:10.2', "COL:10.2",
'UY:1,000.2', "UY:1,000.2",
'ARS:10.123,123', "ARS:10.123,123",
'ARS:1,000,000', "ARS:1,000,000",
'ARSCOL:10', "ARSCOL:10",
'THISISTHEMOTHERCOIN:1,000,000.123,123', "THISISTHEMOTHERCOIN:1,000,000.123,123",
] ];
it('should be valid', () => { it("should be valid", () => {
valids.forEach(v => expect(v).match(AMOUNT_REGEX)) valids.forEach((v) => expect(v).match(AMOUNT_REGEX));
}); });
const invalids = [ const invalids = [
//no currency name //no currency name
':10', ":10",
//use . instead of , //use . instead of ,
'ARS:1.000.000', "ARS:1.000.000",
//currency name with numbers //currency name with numbers
'1ARS:10', "1ARS:10",
//currency name with numbers //currency name with numbers
'AR5:10', "AR5:10",
//missing value //missing value
'USD:', "USD:",
] ];
it('should not be valid', () => { it("should not be valid", () => {
invalids.forEach(v => expect(v).not.match(AMOUNT_REGEX)) invalids.forEach((v) => expect(v).not.match(AMOUNT_REGEX));
}); });
});
})

View File

@ -200,7 +200,7 @@ async function buildRequestOk<T>(
hasToken: boolean, hasToken: boolean,
): Promise<HttpResponseOk<T>> { ): Promise<HttpResponseOk<T>> {
const dataTxt = await response.text(); const dataTxt = await response.text();
const data = dataTxt ? JSON.parse(dataTxt) : undefined const data = dataTxt ? JSON.parse(dataTxt) : undefined;
return { return {
ok: true, ok: true,
data, data,
@ -234,7 +234,7 @@ async function buildRequestFailed(
try { try {
const dataTxt = await response.text(); const dataTxt = await response.text();
const data = dataTxt ? JSON.parse(dataTxt) : undefined const data = dataTxt ? JSON.parse(dataTxt) : undefined;
if (status && status >= 400 && status < 500) { if (status && status >= 400 && status < 500) {
const error: HttpResponseClientError = { const error: HttpResponseClientError = {
clientError: true, clientError: true,