1// Copyright 2021-present 650 Industries. All rights reserved.
2
3#import <XCTest/XCTest.h>
4
5#import <objc/runtime.h>
6#import <EXDevLauncher/EXDevLauncher.h>
7#import <EXDevLauncher/EXDevLauncherController.h>
8
9@interface EXDevLauncherController (EXDevLauncherModuleTests)
10
11- (NSURL *)mockAppManifestURL;
12
13@end
14
15@implementation EXDevLauncherController (EXDevLauncherModuleTests)
16
17- (NSURL *)mockAppManifestURL
18{
19  return [NSURL URLWithString:@"https://exp.host/@test/test?query=param"];
20}
21
22@end
23
24@interface EXDevLauncherModuleTests : XCTestCase
25
26@end
27
28@implementation EXDevLauncherModuleTests
29
30// https://nshipster.com/method-swizzling/
31- (void)swizzleMethodForClass:(Class)class
32             originalSelector:(SEL)originalSelector
33             swizzledSelector:(SEL)swizzledSelector
34{
35  Method originalMethod = class_getInstanceMethod(class, originalSelector);
36  Method swizzledMethod = class_getInstanceMethod(class, swizzledSelector);
37
38  BOOL didAddMethod =
39  class_addMethod(class,
40                  originalSelector,
41                  method_getImplementation(swizzledMethod),
42                  method_getTypeEncoding(swizzledMethod));
43
44  if (didAddMethod) {
45    class_replaceMethod(class,
46                        swizzledSelector,
47                        method_getImplementation(originalMethod),
48                        method_getTypeEncoding(originalMethod));
49  } else {
50    method_exchangeImplementations(originalMethod, swizzledMethod);
51  }
52}
53
54- (void)testConstantsToExportManifestURL
55{
56  // used by snack
57
58  [self swizzleMethodForClass:[EXDevLauncherController class]
59             originalSelector:@selector(appManifestURL)
60             swizzledSelector:@selector(mockAppManifestURL)];
61
62  EXDevLauncher *module = [EXDevLauncher new];
63  NSDictionary *constants = [module constantsToExport];
64  XCTAssertEqualObjects(@"https://exp.host/@test/test?query=param", constants[@"manifestURL"]);
65
66  // clean up
67  [self swizzleMethodForClass:[EXDevLauncherController class]
68             originalSelector:@selector(appManifestURL)
69             swizzledSelector:@selector(mockAppManifestURL)];
70}
71
72@end
73