RACControlCommandExamples.m 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //
  2. // RACControlCommandExamples.m
  3. // ReactiveCocoa
  4. //
  5. // Created by Justin Spahr-Summers on 2013-08-15.
  6. // Copyright (c) 2013 GitHub, Inc. All rights reserved.
  7. //
  8. #import <Quick/Quick.h>
  9. #import <Nimble/Nimble.h>
  10. #import "RACControlCommandExamples.h"
  11. #import "RACCommand.h"
  12. #import "RACSubject.h"
  13. #import "RACUnit.h"
  14. NSString * const RACControlCommandExamples = @"RACControlCommandExamples";
  15. NSString * const RACControlCommandExampleControl = @"RACControlCommandExampleControl";
  16. NSString * const RACControlCommandExampleActivateBlock = @"RACControlCommandExampleActivateBlock";
  17. // Methods used by the unit test that would otherwise require platform-specific
  18. // imports.
  19. @interface NSObject (RACControlCommandExamples)
  20. @property (nonatomic, strong) RACCommand *rac_command;
  21. - (BOOL)isEnabled;
  22. @end
  23. QuickConfigurationBegin(RACControlCommandExampleGroups)
  24. + (void)configure:(Configuration *)configuration {
  25. sharedExamples(RACControlCommandExamples, ^(QCKDSLSharedExampleContext exampleContext) {
  26. __block id control;
  27. __block void (^activate)(id);
  28. __block RACSubject *enabledSubject;
  29. __block RACCommand *command;
  30. qck_beforeEach(^{
  31. control = exampleContext()[RACControlCommandExampleControl];
  32. activate = [exampleContext()[RACControlCommandExampleActivateBlock] copy];
  33. enabledSubject = [RACSubject subject];
  34. command = [[RACCommand alloc] initWithEnabled:enabledSubject signalBlock:^(id sender) {
  35. return [RACSignal return:sender];
  36. }];
  37. [control setRac_command:command];
  38. });
  39. qck_it(@"should bind the control's enabledness to the command", ^{
  40. expect(@([control isEnabled])).toEventually(beTruthy());
  41. [enabledSubject sendNext:@NO];
  42. expect(@([control isEnabled])).toEventually(beFalsy());
  43. [enabledSubject sendNext:@YES];
  44. expect(@([control isEnabled])).toEventually(beTruthy());
  45. });
  46. qck_it(@"should execute the control's command when activated", ^{
  47. __block BOOL executed = NO;
  48. [[command.executionSignals flatten] subscribeNext:^(id sender) {
  49. expect(sender).to(equal(control));
  50. executed = YES;
  51. }];
  52. activate(control);
  53. expect(@(executed)).toEventually(beTruthy());
  54. });
  55. qck_it(@"should overwrite an existing command when setting a new one", ^{
  56. RACCommand *secondCommand = [[RACCommand alloc] initWithSignalBlock:^(id _) {
  57. return [RACSignal return:RACUnit.defaultUnit];
  58. }];
  59. [control setRac_command:secondCommand];
  60. expect([control rac_command]).to(beIdenticalTo(secondCommand));
  61. });
  62. });
  63. }
  64. QuickConfigurationEnd