- refactor for multi-config suport done
- added better errror captures
This commit is contained in:
1 parent
55ea1e23a4
commit
c67d1bd32f
32 files changed
+991
-835
No files matched your search
@@ -18,13 +18,10 @@ export const makeAutomationAPI = (api: API) => ({
|
||||
});
|
||||
return data;
|
||||
},
|
||||
async update({ index, auto }: { index: number; auto: AutomationData }) {
|
||||
async update(auto: AutomationData) {
|
||||
return await api.makeCall({
|
||||
path: AUTOMTAION_ITEM,
|
||||
data: {
|
||||
index,
|
||||
data: auto,
|
||||
},
|
||||
data: auto,
|
||||
});
|
||||
},
|
||||
async updateTags({
|
||||
@@ -42,13 +39,11 @@ export const makeAutomationAPI = (api: API) => ({
|
||||
},
|
||||
});
|
||||
},
|
||||
async remove({ index }: { index: number }) {
|
||||
async remove(auto: AutomationData) {
|
||||
return await api.makeCall({
|
||||
method: "DELETE",
|
||||
path: AUTOMTAION_ITEM,
|
||||
data: {
|
||||
index,
|
||||
},
|
||||
data: auto,
|
||||
});
|
||||
},
|
||||
});
|
||||
|
||||
@@ -24,12 +24,12 @@ test("mock api uses initial autos for initial population -- some data", async ()
|
||||
|
||||
test("populate mock api with some data", async () => {
|
||||
const mockApi = makeAutomationAPI(useMockAPI([], fakeUseRef as any));
|
||||
await mockApi.update({ index: 0, auto: createMockAuto() });
|
||||
await mockApi.update(createMockAuto());
|
||||
let data = await mockApi.list({ limit: 10, offset: 0 });
|
||||
expect(data.ok).toBe(true);
|
||||
expect((data as any).data.data).toHaveLength(1);
|
||||
expect((data as any).data.totalItems).toBe(1);
|
||||
await mockApi.update({ index: 1, auto: createMockAuto() });
|
||||
await mockApi.update(createMockAuto());
|
||||
data = await mockApi.list({ limit: 10, offset: 0 });
|
||||
expect((data as any).data.data).toHaveLength(2);
|
||||
expect((data as any).data.totalItems).toBe(2);
|
||||
@@ -50,13 +50,13 @@ test("delete/update data from mock api", async () => {
|
||||
);
|
||||
let data = ((await mockApi.list({ limit: 10, offset: 0 })) as any).data.data;
|
||||
const toBeEdited = data[0];
|
||||
toBeEdited.metadata.alias = "Bob is my uncle";
|
||||
await mockApi.update({ index: 0, auto: toBeEdited });
|
||||
toBeEdited.alias = "Bob is my uncle";
|
||||
await mockApi.update(toBeEdited);
|
||||
data = ((await mockApi.list({ limit: 10, offset: 0 })) as any).data.data;
|
||||
expect(data[0].metadata.alias).toEqual("Bob is my uncle");
|
||||
await mockApi.remove({ index: 0 });
|
||||
expect(data[0].alias).toEqual("Bob is my uncle");
|
||||
await mockApi.remove(data[0]);
|
||||
data = ((await mockApi.list({ limit: 10, offset: 0 })) as any).data.data;
|
||||
expect(data[0].metadata.alias === "Bob is my uncle").toEqual(false);
|
||||
expect(data[0].alias === "Bob is my uncle").toEqual(false);
|
||||
});
|
||||
|
||||
test("update tags from mock api", async () => {
|
||||
@@ -74,7 +74,7 @@ test("update tags from mock api", async () => {
|
||||
);
|
||||
let data = ((await mockApi.list({ limit: 10, offset: 0 })) as any).data
|
||||
.data as AutomationData[];
|
||||
const autoId = data[0].metadata.id;
|
||||
const autoId = data[0].id;
|
||||
expect(data[0].tags).toStrictEqual({});
|
||||
mockApi.updateTags({
|
||||
automation_id: autoId,
|
||||
@@ -85,7 +85,7 @@ test("update tags from mock api", async () => {
|
||||
});
|
||||
data = ((await mockApi.list({ limit: 10, offset: 0 })) as any).data
|
||||
.data as AutomationData[];
|
||||
expect(data[0].metadata.id).toEqual(autoId);
|
||||
expect(data[0].id).toEqual(autoId);
|
||||
expect(data[0].tags).toStrictEqual({
|
||||
room: "bathroom",
|
||||
type: "popup",
|
||||
|
||||
@@ -36,8 +36,11 @@ export const useMockAPI = (
|
||||
}
|
||||
if (path === AUTOMTAION_ITEM) {
|
||||
if (method === "POST") {
|
||||
const { index, data: auto } = data as any;
|
||||
if (index >= automationsRef.current.length) {
|
||||
const auto = data as AutomationData;
|
||||
const index = automationsRef.current.findIndex(
|
||||
({ id }) => auto.id === id
|
||||
);
|
||||
if (index < 0) {
|
||||
automationsRef.current = [...automationsRef.current, auto];
|
||||
} else {
|
||||
automationsRef.current = [
|
||||
@@ -51,11 +54,10 @@ export const useMockAPI = (
|
||||
data: {},
|
||||
};
|
||||
} else if (method === "DELETE") {
|
||||
const { index } = data as any;
|
||||
automationsRef.current = [
|
||||
...automationsRef.current.slice(0, index),
|
||||
...automationsRef.current.slice(index + 1),
|
||||
];
|
||||
const auto = data as AutomationData;
|
||||
automationsRef.current = automationsRef.current.filter(
|
||||
({ id }) => id !== auto.id
|
||||
);
|
||||
return {
|
||||
ok: true,
|
||||
data: {},
|
||||
@@ -67,7 +69,7 @@ export const useMockAPI = (
|
||||
const { automation_id, tags } = data as any;
|
||||
for (let i = 0; i < automationsRef.current.length; i++) {
|
||||
const auto = automationsRef.current[i];
|
||||
if (auto.metadata.id === automation_id) {
|
||||
if (auto.id === automation_id) {
|
||||
automationsRef.current[i].tags = JSON.parse(JSON.stringify(tags));
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user