1 import ExpoModulesTestCore 2 3 @testable import ExpoModulesCore 4 5 class RecordSpec: ExpoSpec { specnull6 override func spec() { 7 let appContext = AppContext.create() 8 9 it("initializes with empty dictionary") { 10 struct TestRecord: Record { } 11 _ = try TestRecord(from: [:], appContext: appContext) 12 } 13 14 it("works back and forth with a field") { 15 struct TestRecord: Record { 16 @Field var a: String? 17 } 18 let dict = ["a": "b"] 19 let record = try TestRecord(from: dict, appContext: appContext) 20 21 expect(record.a).to(be(dict["a"])) 22 expect(record.toDictionary()["a"]).to(be(dict["a"])) 23 } 24 25 it("works back and forth with a keyed field") { 26 struct TestRecord: Record { 27 @Field("key") var a: String? 28 } 29 let dict = ["key": "b"] 30 let record = try TestRecord(from: dict, appContext: appContext) 31 32 expect(record.a).to(be(dict["key"])) 33 expect(record.toDictionary()["key"]).to(be(dict["key"])) 34 } 35 36 it("throws when required field is missing") { 37 struct TestRecord: Record { 38 @Field(.required) var a: Int 39 } 40 41 expect { try TestRecord(from: [:], appContext: appContext) }.to(throwError { error in 42 expect(error).to(beAKindOf(FieldRequiredException.self)) 43 }) 44 } 45 46 it("throws when casting is not possible") { 47 struct TestRecord: Record { 48 @Field var a: Int 49 } 50 let dict = ["a": "try with String instead of Int"] 51 52 expect { try TestRecord(from: dict, appContext: appContext) }.to(throwError { error in 53 expect(error).to(beAKindOf(FieldInvalidTypeException.self)) 54 }) 55 } 56 } 57 } 58