|
import { Expose, instanceToPlain, plainToInstance } from "class-transformer"; |
|
import { ToMap } from "./src"; |
|
|
|
class Item { |
|
constructor(prop: number) { |
|
this.prop = prop; |
|
} |
|
|
|
@Expose() |
|
prop!: number; |
|
} |
|
|
|
class TestNumberModel { |
|
@Expose() |
|
@ToMap({ mapValueClass: Number }) |
|
prop!: Map<string, number>; |
|
} |
|
|
|
class TestStringModel { |
|
@Expose() |
|
@ToMap({ mapValueClass: String }) |
|
prop!: Map<string, string>; |
|
} |
|
|
|
class TestBooleanModel { |
|
@Expose() |
|
@ToMap({ mapValueClass: Boolean }) |
|
prop!: Map<string, boolean>; |
|
} |
|
|
|
class TestItemModel { |
|
@Expose() |
|
@ToMap({ mapValueClass: Item }) |
|
prop!: Map<string, Item>; |
|
} |
|
|
|
describe(ToMap.name, () => { |
|
describe("should work with Number value class", () => { |
|
const plain = { |
|
prop: { |
|
"1": 11, |
|
"2": 22, |
|
}, |
|
}; |
|
|
|
it("should transform Record<string, number> to Map<string, number>", () => { |
|
return expect(plainToInstance(TestNumberModel, plain).prop).toEqual( |
|
(new TestNumberModel().prop = new Map([ |
|
["1", 11], |
|
["2", 22], |
|
])) |
|
); |
|
}); |
|
|
|
it("should transform Map<string, number> to Record<string, number>", () => { |
|
return expect(instanceToPlain(plainToInstance(TestNumberModel, plain))).toEqual(plain); |
|
}); |
|
}); |
|
|
|
describe("should work with String value class", () => { |
|
const plain = { |
|
prop: { |
|
"1": "11", |
|
"2": "22", |
|
}, |
|
}; |
|
|
|
it("should transform Record<string, string> to Map<string, string>", () => { |
|
return expect(plainToInstance(TestStringModel, plain).prop).toEqual( |
|
(new TestStringModel().prop = new Map([ |
|
["1", "11"], |
|
["2", "22"], |
|
])) |
|
); |
|
}); |
|
|
|
it("should transform Map<string, string> to Record<string, string>", () => { |
|
return expect(instanceToPlain(plainToInstance(TestStringModel, plain))).toEqual(plain); |
|
}); |
|
}); |
|
|
|
describe("should work with Boolean value class", () => { |
|
const plain = { |
|
prop: { |
|
"1": true, |
|
"2": false, |
|
}, |
|
}; |
|
|
|
it("should transform Record<string, boolean> to Map<string, boolean>", () => { |
|
return expect(plainToInstance(TestBooleanModel, plain).prop).toEqual( |
|
(new TestBooleanModel().prop = new Map([ |
|
["1", true], |
|
["2", false], |
|
])) |
|
); |
|
}); |
|
|
|
it("should transform Map<string, boolean> to Record<string, boolean>", () => { |
|
return expect(instanceToPlain(plainToInstance(TestBooleanModel, plain))).toEqual(plain); |
|
}); |
|
}); |
|
|
|
describe("should work with custom value class", () => { |
|
const plain = { |
|
prop: { |
|
"1": { prop: 11 }, |
|
"2": { prop: 22 }, |
|
}, |
|
}; |
|
|
|
it("should transform Record<string, Item> to Map<string, Item>", () => { |
|
return expect(plainToInstance(TestItemModel, plain).prop).toEqual( |
|
(new TestItemModel().prop = new Map([ |
|
["1", new Item(11)], |
|
["2", new Item(22)], |
|
])) |
|
); |
|
}); |
|
|
|
it("should transform Map<string, Item> to Record<string, Item>", () => { |
|
return expect(instanceToPlain(plainToInstance(TestItemModel, plain))).toEqual(plain); |
|
}); |
|
}); |
|
}); |
@ruscon
Thank you for your comment. ๐
I've created a new revision based on the refactored version you wrote.
And I also put your name in the contributors section of package.json.
Besides I'm realizing that it's better to maintain this in npm rather than gist. ๐