Skip to content

Commit 27992b6

Browse files
committed
add tests for normalize functions
1 parent dee6be1 commit 27992b6

File tree

2 files changed

+69
-2
lines changed

2 files changed

+69
-2
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { describe, expect, it } from 'vitest';
2+
import { XcmVersion } from './ExtrinsicBuilder.interfaces';
3+
import {
4+
applyConcreteWrapper,
5+
normalizeConcrete,
6+
normalizeX1,
7+
} from './ExtrinsicBuilder.utils';
8+
9+
describe('normalizeX1', () => {
10+
it('should return original object when xcmVersion < v4', () => {
11+
const input = {
12+
interior: { X1: { Parachain: 1000 } },
13+
};
14+
15+
expect(normalizeX1(XcmVersion.v3, input)).toEqual(input);
16+
});
17+
18+
it('should wrap X1 value in array for xcmVersion >= v4', () => {
19+
const input = {
20+
interior: { X1: { Parachain: 1000 } },
21+
};
22+
const expected = {
23+
interior: { X1: [{ Parachain: 1000 }] },
24+
};
25+
26+
expect(normalizeX1(XcmVersion.v4, input)).toEqual(expected);
27+
});
28+
29+
it('should handle lowercase x1 key', () => {
30+
const input = {
31+
interior: { x1: { Parachain: 1000 } },
32+
};
33+
const expected = {
34+
interior: { x1: [{ Parachain: 1000 }] },
35+
};
36+
37+
expect(normalizeX1(XcmVersion.v4, input)).toEqual(expected);
38+
});
39+
40+
it('should not modify object without X1/x1 key', () => {
41+
const input = {
42+
interior: { X2: [{ Parachain: 1000 }, { GeneralIndex: 1 }] },
43+
};
44+
45+
expect(normalizeX1(XcmVersion.v4, input)).toEqual(input);
46+
});
47+
});
48+
49+
describe('normalizeConcrete', () => {
50+
it('should return original object for xcmVersion >= v4', () => {
51+
const input = { Parachain: 1000 };
52+
53+
expect(normalizeConcrete(XcmVersion.v4, input)).toBe(input);
54+
expect(normalizeConcrete(XcmVersion.v5, input)).toBe(input);
55+
});
56+
57+
it('should wrap object in Concrete for xcmVersion < v4', () => {
58+
const input = { Parachain: 1000 };
59+
const expected = { Concrete: { Parachain: 1000 } };
60+
61+
expect(normalizeConcrete(XcmVersion.v3, input)).toEqual(expected);
62+
expect(normalizeConcrete(XcmVersion.v2, input)).toEqual(expected);
63+
expect(normalizeConcrete(XcmVersion.v1, input)).toEqual(expected);
64+
});
65+
});

packages/builder/src/extrinsic/ExtrinsicBuilder.utils.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,12 @@ export function normalizeConcrete(
9191
xcmVersion: XcmVersion,
9292
versionedObject: object,
9393
) {
94-
return isXcmV4(xcmVersion) ? versionedObject : applyConcreteWrapper;
94+
return isXcmV4(xcmVersion)
95+
? versionedObject
96+
: applyConcreteWrapper(versionedObject);
9597
}
9698

97-
function applyConcreteWrapper(versionedObject: object) {
99+
export function applyConcreteWrapper(versionedObject: object) {
98100
return {
99101
Concrete: { ...versionedObject },
100102
};

0 commit comments

Comments
 (0)