| 1 |
import { expect, test } from "@odoo/hoot"; |
| 2 |
import { SERIALIZED_UI_STATE_PROP } from "@point_of_sale/app/models/related_models/utils"; |
| 3 |
import { getRelatedModelsInstance } from "../data/get_model_definitions"; |
| 4 |
import { makeMockServer } from "@web/../tests/web_test_helpers"; |
| 5 |
import { definePosModels } from "../data/generate_model_definitions"; |
| 6 |
import { getFilledOrder, setupPosEnv } from "../utils"; |
| 7 |
|
| 8 |
definePosModels(); |
| 9 |
|
| 10 |
test("newly created record", async () => { |
| 11 |
await makeMockServer(); |
| 12 |
const models = getRelatedModelsInstance(false); |
| 13 |
const order = models["pos.order"].create({ amount_total: 10 }); |
| 14 |
const line1 = models["pos.order.line"].create({ |
| 15 |
order_id: order, |
| 16 |
qty: 1, |
| 17 |
}); |
| 18 |
const line2 = models["pos.order.line"].create({ |
| 19 |
order_id: order, |
| 20 |
qty: 2, |
| 21 |
}); |
| 22 |
{ |
| 23 |
const result = order.serializeForIndexedDB(); |
| 24 |
expect(result.id).toBe(order.id); |
| 25 |
expect(result.amount_total).toBe(order.amount_total); |
| 26 |
expect(Array.isArray(result.lines)).toBe(true); |
| 27 |
expect(result.lines.length).toBe(2); |
| 28 |
expect(result.lines[0]).toBe(line1.id); |
| 29 |
expect(result.lines[1]).toBe(line2.id); |
| 30 |
expect(result[SERIALIZED_UI_STATE_PROP]).toBeEmpty(); |
| 31 |
} |
| 32 |
|
| 33 |
{ |
| 34 |
const result = line1.serializeForIndexedDB(); |
| 35 |
expect(result.id).toBe(line1.id); |
| 36 |
expect(result.qty).toBe(1); |
| 37 |
expect(result[SERIALIZED_UI_STATE_PROP]).toBeEmpty(); |
| 38 |
} |
| 39 |
}); |
| 40 |
|
| 41 |
test("UIState serialization", async () => { |
| 42 |
await makeMockServer(); |
| 43 |
const models = getRelatedModelsInstance(false); |
| 44 |
const order = models["pos.order"].create({ amount_total: 10 }); |
| 45 |
order.uiState = { demoValue: 99 }; |
| 46 |
|
| 47 |
const result = order.serializeForIndexedDB(); |
| 48 |
expect(result.id).toBe(order.id); |
| 49 |
expect(result.amount_total).toBe(10); |
| 50 |
expect(result[SERIALIZED_UI_STATE_PROP]).not.toBeEmpty(); |
| 51 |
expect(typeof result[SERIALIZED_UI_STATE_PROP]).toBe("string"); |
| 52 |
}); |
| 53 |
|
| 54 |
test("Restore serialized data", async () => { |
| 55 |
const store = await setupPosEnv(); |
| 56 |
const order = await getFilledOrder(store); |
| 57 |
order.uiState = { demoValue: 999 }; |
| 58 |
const serialized = order.serializeForIndexedDB(); |
| 59 |
const serializedLines = order.lines.map((line) => line.serializeForIndexedDB()); |
| 60 |
|
| 61 |
store.data.localDeleteCascade(order); |
| 62 |
const data = store.models.loadConnectedData({ |
| 63 |
"pos.order": [serialized], |
| 64 |
"pos.order.line": serializedLines, |
| 65 |
}); |
| 66 |
|
| 67 |
// UI state is restored |
| 68 |
expect(data["pos.order"][0].uiState.demoValue).toBe(999); |
| 69 |
// UIState must be excluded from the raw data |
| 70 |
expect(data["pos.order"][0].raw.uiState).toBeEmpty(); |
| 71 |
}); |