scenario_spec.rb 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. require 'rails_helper'
  2. describe Scenario do
  3. let(:new_instance) { users(:bob).scenarios.build(:name => "some scenario") }
  4. it_behaves_like HasGuid
  5. describe "validations" do
  6. before do
  7. expect(new_instance).to be_valid
  8. end
  9. it "validates the presence of name" do
  10. new_instance.name = ''
  11. expect(new_instance).not_to be_valid
  12. end
  13. it "validates the presence of user" do
  14. new_instance.user = nil
  15. expect(new_instance).not_to be_valid
  16. end
  17. it "validates tag_fg_color is hex color" do
  18. new_instance.tag_fg_color = '#N07H3X'
  19. expect(new_instance).not_to be_valid
  20. new_instance.tag_fg_color = '#BADA55'
  21. expect(new_instance).to be_valid
  22. end
  23. it "allows nil tag_fg_color" do
  24. new_instance.tag_fg_color = nil
  25. expect(new_instance).to be_valid
  26. end
  27. it "validates tag_bg_color is hex color" do
  28. new_instance.tag_bg_color = '#N07H3X'
  29. expect(new_instance).not_to be_valid
  30. new_instance.tag_bg_color = '#BADA55'
  31. expect(new_instance).to be_valid
  32. end
  33. it "allows nil tag_bg_color" do
  34. new_instance.tag_bg_color = nil
  35. expect(new_instance).to be_valid
  36. end
  37. it "only allows Agents owned by user" do
  38. new_instance.agent_ids = [agents(:bob_website_agent).id]
  39. expect(new_instance).to be_valid
  40. new_instance.agent_ids = [agents(:jane_website_agent).id]
  41. expect(new_instance).not_to be_valid
  42. end
  43. end
  44. describe "counters" do
  45. it "maintains a counter cache on user" do
  46. expect {
  47. new_instance.save!
  48. }.to change { users(:bob).reload.scenario_count }.by(1)
  49. expect {
  50. new_instance.destroy
  51. }.to change { users(:bob).reload.scenario_count }.by(-1)
  52. end
  53. end
  54. context '#unique_agents' do
  55. it "equals agents when no agents are shared" do
  56. agent_ids = scenarios(:bob_weather).agents.map(&:id).sort
  57. unique_agent_ids = scenarios(:bob_weather).send(:unique_agent_ids).sort
  58. expect(agent_ids).to eq(unique_agent_ids)
  59. end
  60. it "includes only agents that are not present in two scnearios" do
  61. unique_agent_ids = scenarios(:jane_weather).send(:unique_agent_ids)
  62. expect(unique_agent_ids).to eq([agents(:jane_rain_notifier_agent).id])
  63. end
  64. it "returns no agents when all are also used in a different scenario" do
  65. expect(scenarios(:jane_weather_duplicate).send(:unique_agent_ids)).to eq([])
  66. end
  67. end
  68. context '#destroy_with_mode' do
  69. it "only destroys the scenario when no mode is passed" do
  70. expect { scenarios(:jane_weather).destroy_with_mode('') }.not_to change(Agent, :count)
  71. end
  72. it "only destroys unique agents when 'unique_agents' is passed" do
  73. expect { scenarios(:jane_weather).destroy_with_mode('unique_agents') }.to change(Agent, :count).by(-1)
  74. end
  75. it "destroys all agents when 'all_agents' is passed" do
  76. expect { scenarios(:jane_weather).destroy_with_mode('all_agents') }.to change(Agent, :count).by(-2)
  77. end
  78. end
  79. end