/
LocalizationTests.swift
113 lines (84 loc) · 4.78 KB
/
LocalizationTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//
// LocalizationTests.swift
// Contentful
//
// Created by JP Wright on 06.06.17.
// Copyright © 2017 Contentful GmbH. All rights reserved.
//
@testable import Contentful
import XCTest
import Nimble
import DVR
struct LocaleFactory {
static func enUSDefault() -> Contentful.Locale {
let usLocaleJSONData = JSONDecodingTests.jsonData("en-US-locale")
let locale = try! JSONDecoder.withoutLocalizationContext().decode(Contentful.Locale.self, from: usLocaleJSONData)
return locale
}
static func klingonWithUSFallback() -> Contentful.Locale {
let tlhLocaleJSONData = JSONDecodingTests.jsonData("tlh-locale")
let locale = try! JSONDecoder.withoutLocalizationContext().decode(Contentful.Locale.self, from: tlhLocaleJSONData)
return locale
}
}
class LocalizationTests: XCTestCase {
static let client = TestClientFactory.testClient(withCassetteNamed: "EntryTests")
override class func setUp() {
super.setUp()
(client.urlSession as? DVR.Session)?.beginRecording()
}
override class func tearDown() {
super.tearDown()
(client.urlSession as? DVR.Session)?.endRecording()
}
func testNormalizingFieldsDictionaryFormat() {
let singleLocaleJSONData = JSONDecodingTests.jsonData("fields-for-default-locale")
let singleLocaleJSON = try! JSONSerialization.jsonObject(with: singleLocaleJSONData, options: []) as! [String: Any]
let singleLocaleFields = singleLocaleJSON["fields"] as! [String: Any]
let enUSLocale = LocaleFactory.enUSDefault()
let singleLocaleNormalizedFields = try! Localization.fieldsInMultiLocaleFormat(from: singleLocaleFields, selectedLocale: enUSLocale, wasSelectedOnAPILevel: true)
expect((singleLocaleNormalizedFields["name"]?["en-US"] as! String)).to(equal("Happy Cat"))
expect(singleLocaleNormalizedFields["name"]?["tlh"]).to(beNil())
// Multi locale format.
let multiLocaleJSONData = JSONDecodingTests.jsonData("fields-in-mulit-locale-format")
let multiLocaleJSON = try! JSONSerialization.jsonObject(with: multiLocaleJSONData, options: []) as! [String: Any]
let multiLocaleFields = multiLocaleJSON["fields"] as! [String: Any]
let multiLocaleNormalizedFields = try! Localization.fieldsInMultiLocaleFormat(from: multiLocaleFields, selectedLocale: enUSLocale, wasSelectedOnAPILevel: false)
expect((multiLocaleNormalizedFields["name"]?["en-US"] as! String)).to(equal("Happy Cat"))
expect(multiLocaleNormalizedFields["name"]?["tlh"]).toNot(beNil())
expect((multiLocaleNormalizedFields["name"]?["tlh"] as! String)).to(equal("Quch vIghro"))
}
func testWalkingFallbackChain() {
let expecatation = self.expectation(description: "Entries matching query network expectation")
LocalizationTests.client.fetchEntries(matching: Query.where(sys: .id, .equals("nyancat")).localizeResults(withLocaleCode: "*")).then { entriesArrayResponse in
let entry = entriesArrayResponse.items.first!
expect(entry.currentlySelectedLocale.code).to(equal("en-US"))
expect(entry.sys.id).to(equal("nyancat"))
expect(entry.fields["name"] as? String).to(equal("Nyan Cat"))
expect(entry.fields["likes"] as? [String]).to(equal(["rainbows", "fish"]))
// Set new locale.
entry.setLocale(withCode: "tlh")
expect(entry.currentlySelectedLocale.code).to(equal("tlh"))
expect(entry.fields["name"] as? String).to(equal("Nyan vIghro'"))
// fields with no value for "tlh" should fallback.
expect(entry.fields["likes"] as? [String]).to(equal(["rainbows", "fish"]))
expecatation.fulfill()
}.error {
fail("\($0)")
expecatation.fulfill()
}
waitForExpectations(timeout: 10.0, handler: nil)
}
func testWalkingFallbackchainOnAsset() {
let jsonDecoder = JSONDecoder.withoutLocalizationContext()
let localesJSONData = JSONDecodingTests.jsonData("all-locales")
let localesResponse = try! jsonDecoder.decode(ArrayResponse<Contentful.Locale>.self, from: localesJSONData)
jsonDecoder.update(with: LocalizationContext(locales: localesResponse.items)!)
let assetJSONData = JSONDecodingTests.jsonData("localizable-asset")
let asset = try! jsonDecoder.decode(Asset.self, from: assetJSONData)
expect(asset.sys.id).to(equal("1x0xpXu4pSGS4OukSyWGUK"))
expect(asset.urlString).to(equal("https://images.ctfassets.net/cfexampleapi/1x0xpXu4pSGS4OukSyWGUK/cc1239c6385428ef26f4180190532818/doge.jpg"))
asset.setLocale(withCode: "tlh")
expect(asset.urlString).to(equal("https://images.ctfassets.net/cfexampleapi/1x0xpXu4pSGS4OukSyWGUK/cc1239c6385428ef26f4180190532818/doge.jpg"))
}
}