|
| 1 | +// |
| 2 | +// GlobalFieldTest.m |
| 3 | +// Contentstack |
| 4 | +// |
| 5 | +// Created by Reeshika Hosmani on 02/06/25. |
| 6 | +// Copyright © 2025 Contentstack. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import <XCTest/XCTest.h> |
| 10 | +#import <Contentstack/Contentstack.h> |
| 11 | +#import "CSIOInternalHeaders.h" |
| 12 | +#import "GlobalField.h" |
| 13 | +#import "ContentstackDefinitions.h" |
| 14 | + |
| 15 | +@interface GlobalFieldTest : XCTestCase{ |
| 16 | + Stack *csStack; |
| 17 | + Config *config; |
| 18 | +} |
| 19 | + |
| 20 | +@property (nonatomic, strong) Stack *stack; |
| 21 | +@property (nonatomic, strong) GlobalField *globalField; |
| 22 | + |
| 23 | +@end |
| 24 | + |
| 25 | +@implementation GlobalFieldTest |
| 26 | + |
| 27 | +- (void)setUp { |
| 28 | + [super setUp]; |
| 29 | + NSString *path = [[NSBundle bundleForClass:self.class] pathForResource:@"config" ofType:@"json"]; |
| 30 | + NSData *data = [NSData dataWithContentsOfFile:path]; |
| 31 | + NSDictionary *configdict = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil]; |
| 32 | + self.stack = [Contentstack stackWithAPIKey:configdict[@"api_key"] |
| 33 | + accessToken:configdict[@"delivery_token"] |
| 34 | + environmentName:configdict[@"environment"]]; |
| 35 | + // Initialize global field |
| 36 | + self.globalField = [self.stack globalFieldWithName:@"global_field_uid"]; |
| 37 | +} |
| 38 | + |
| 39 | +- (void)tearDown { |
| 40 | + self.stack = nil; |
| 41 | + self.globalField = nil; |
| 42 | + [super tearDown]; |
| 43 | +} |
| 44 | + |
| 45 | +- (void)testFetchGlobalField { |
| 46 | + XCTestExpectation *expectation = [self expectationWithDescription:@"Fetch GlobalField"]; |
| 47 | + |
| 48 | + [self.globalField includeBranch]; |
| 49 | + |
| 50 | + // Call fetch method |
| 51 | + [self.globalField fetch:^(ResponseType type, NSError * _Nullable error) { |
| 52 | + if (error) { |
| 53 | + XCTFail(@"Error fetching global field: %@", error); |
| 54 | + } |
| 55 | + |
| 56 | + // Verify no error |
| 57 | + XCTAssertNil(error, @"Error should be nil"); |
| 58 | + |
| 59 | + // Verify globalField properties |
| 60 | + XCTAssertNotNil(self.globalField.title, @"Title should not be nil"); |
| 61 | + XCTAssertNotNil(self.globalField.uid, @"UID should not be nil"); |
| 62 | + XCTAssertNotNil(self.globalField.description, @"Description should not be nil"); |
| 63 | + |
| 64 | + // Verify data types |
| 65 | + XCTAssertTrue([self.globalField.title isKindOfClass:[NSString class]], @"Title should be NSString"); |
| 66 | + XCTAssertTrue([self.globalField.uid isKindOfClass:[NSString class]], @"UID should be NSString"); |
| 67 | + XCTAssertTrue([self.globalField.description isKindOfClass:[NSString class]], @"Description should be NSString"); |
| 68 | + |
| 69 | + [expectation fulfill]; |
| 70 | + }]; |
| 71 | + |
| 72 | + // Wait for expectation |
| 73 | + [self waitForExpectationsWithTimeout:10.0 handler:^(NSError * _Nullable error) { |
| 74 | + if (error) { |
| 75 | + NSLog(@"Test timeout error: %@", error); |
| 76 | + XCTFail(@"Expectation failed with error: %@", error); |
| 77 | + } |
| 78 | + }]; |
| 79 | +} |
| 80 | + |
| 81 | +- (void)testFetchGlobalFieldWithInvalidName { |
| 82 | + XCTestExpectation *expectation = [self expectationWithDescription:@"Fetch Invalid GlobalField"]; |
| 83 | + |
| 84 | + // Create globalField with invalid name |
| 85 | + GlobalField *invalidGlobalField = [self.stack globalFieldWithName:@"invalid_global_field"]; |
| 86 | + |
| 87 | + // Call fetch method |
| 88 | + [invalidGlobalField fetch:^(ResponseType type, NSError * _Nullable error) { |
| 89 | + // Verify error is not nil |
| 90 | + XCTAssertNotNil(error, @"Error should not be nil"); |
| 91 | + |
| 92 | + [expectation fulfill]; |
| 93 | + }]; |
| 94 | + |
| 95 | + // Wait for expectation |
| 96 | + [self waitForExpectationsWithTimeout:10.0 handler:^(NSError * _Nullable error) { |
| 97 | + if (error) { |
| 98 | + XCTFail(@"Expectation failed with error: %@", error); |
| 99 | + } |
| 100 | + }]; |
| 101 | +} |
| 102 | + |
| 103 | +- (void)testFetchGlobalFieldWithIncludeBranch { |
| 104 | + XCTestExpectation *expectation = [self expectationWithDescription:@"Fetch GlobalField with Branch"]; |
| 105 | + |
| 106 | + // Include branch |
| 107 | + [self.globalField includeBranch]; |
| 108 | + |
| 109 | + // Call fetch method |
| 110 | + [self.globalField fetch:^(ResponseType type, NSError * _Nullable error) { |
| 111 | + // Verify no error |
| 112 | + XCTAssertNil(error, @"Error should be nil"); |
| 113 | + |
| 114 | + // Verify branch is included |
| 115 | + XCTAssertNotNil(self.globalField.branch, @"Branch should not be nil"); |
| 116 | + |
| 117 | + [expectation fulfill]; |
| 118 | + }]; |
| 119 | + |
| 120 | + // Wait for expectation |
| 121 | + [self waitForExpectationsWithTimeout:10.0 handler:^(NSError * _Nullable error) { |
| 122 | + if (error) { |
| 123 | + XCTFail(@"Expectation failed with error: %@", error); |
| 124 | + } |
| 125 | + }]; |
| 126 | +} |
| 127 | + |
| 128 | +- (void)testFetchGlobalFieldWithIncludeSchema { |
| 129 | + XCTestExpectation *expectation = [self expectationWithDescription:@"Fetch GlobalField with Schema"]; |
| 130 | + |
| 131 | + // Include schema |
| 132 | + [self.globalField includeGlobalFieldSchema]; |
| 133 | + |
| 134 | + // Call fetch method |
| 135 | + [self.globalField fetch:^(ResponseType type, NSError * _Nullable error) { |
| 136 | + // Verify no error |
| 137 | + XCTAssertNil(error, @"Error should be nil"); |
| 138 | + |
| 139 | + // Verify schema is included in properties |
| 140 | + XCTAssertNotNil(self.globalField.schema, @"Schema should not be nil"); |
| 141 | + |
| 142 | + [expectation fulfill]; |
| 143 | + }]; |
| 144 | + |
| 145 | + // Wait for expectation |
| 146 | + [self waitForExpectationsWithTimeout:10.0 handler:^(NSError * _Nullable error) { |
| 147 | + if (error) { |
| 148 | + XCTFail(@"Expectation failed with error: %@", error); |
| 149 | + } |
| 150 | + }]; |
| 151 | +} |
| 152 | + |
| 153 | +- (void)testFetchAllGlobalFields { |
| 154 | + XCTestExpectation *expectation = [self expectationWithDescription:@"Fetch all GlobalFields"]; |
| 155 | + |
| 156 | + // Call fetch method |
| 157 | + [self.globalField fetchAll:^(ResponseType type, NSArray<GlobalField *> * _Nullable result, NSError * _Nullable error) { |
| 158 | + |
| 159 | + // Verify no error |
| 160 | + XCTAssertNil(error, @"Error should be nil"); |
| 161 | + XCTAssertNotNil(result, @"Result is not null"); |
| 162 | + [expectation fulfill]; |
| 163 | + |
| 164 | + }]; |
| 165 | + |
| 166 | + // Wait for expectation |
| 167 | + [self waitForExpectationsWithTimeout:10.0 handler:^(NSError * _Nullable error) { |
| 168 | + if (error) { |
| 169 | + XCTFail(@"Expectation failed with error: %@", error); |
| 170 | + } |
| 171 | + }]; |
| 172 | +} |
| 173 | + |
| 174 | +@end |
0 commit comments