require 'spec_helper' describe LiveQA::Event do describe '.configurations' do it { expect(LiveQA.configurations).to be_a(LiveQA::Config) } end describe '.track' do let(:expected_arg) {{ user_id: 42, type: 'track', name: 'test', message_id: kind_of(String), timestamp: kind_of(String), session_tracker_id: kind_of(String), properties: { total: 10 }, library: { name: "liveqa", language: "ruby", version: kind_of(String) }, server: { host: kind_of(String), pid: kind_of(Numeric) }, }} let(:response) { double('LiveQA::Event', successful?: true) } context 'async' do let(:processor_mock) { double('Processor', enqueue: true) } before do allow(LiveQA).to receive(:processor).and_return(processor_mock) LiveQA.track('test', { user_id: 42, properties: { total: 10 }}, no_ssl: true, space_name: 'test') end it { expect(processor_mock).to have_received(:enqueue).with(action: 'events:create', payload: hash_including(expected_arg), options: { space_name: 'test' }) } end context 'without async' do before do LiveQA.configurations.async = false allow(LiveQA::Event).to receive(:create).and_return(response) LiveQA.track('test', { user_id: 42, properties: { total: 10 }}, no_ssl: true, space_name: 'test') end it { expect(LiveQA::Event).to have_received(:create).with(hash_including(expected_arg), { no_ssl: true, space_name: 'test' }) } end context 'not enabled' do before do LiveQA.configurations.enabled = false LiveQA.track('test', { user_id: 42, properties: { total: 10 }}, no_ssl: true, space_name: 'test') end it { expect(LiveQA::Event).to_not receive(:create) } end end describe '.identify' do let(:expected_arg) {{ tracker_id: 'xx', properties: { name: 'John Doe' }, type: 'identify', user_id: 42, message_id: kind_of(String), timestamp: kind_of(String), session_tracker_id: kind_of(String), library: { name: "liveqa", language: "ruby", version: kind_of(String) }, server: { host: kind_of(String), pid: kind_of(Numeric) }, }} let(:response) { double('LiveQA::Event', successful?: true) } context 'async' do let(:processor_mock) { double('Processor', enqueue: true) } before do allow(LiveQA).to receive(:processor).and_return(processor_mock) LiveQA.identify(42, { tracker_id: 'xx', properties: { name: 'John Doe' }}, no_ssl: true, space_name: 'test') end it { expect(processor_mock).to have_received(:enqueue).with(action: 'events:create', payload: hash_including(expected_arg), options: { space_name: 'test' }) } end context 'without async' do before do LiveQA.configurations.async = false allow(LiveQA::Event).to receive(:create).and_return(response) LiveQA.identify(42, { tracker_id: 'xx', properties: { name: 'John Doe' }}, no_ssl: true, space_name: 'test') end it { expect(LiveQA::Event).to have_received(:create).with(hash_including(expected_arg), { no_ssl: true, space_name: 'test' }) } end context 'not enabled' do before do LiveQA.configurations.enabled = false LiveQA.identify(42, { tracker_id: 'xx', properties: { name: 'John Doe' }}, no_ssl: true, space_name: 'test') end it { expect(LiveQA::Event).to_not receive(:create) } end end describe '.set_group' do let(:expected_arg) {{ properties: { name: 'John Group' }, message_id: kind_of(String), timestamp: kind_of(String), session_tracker_id: kind_of(String), }} let(:response) { double('LiveQA::Group', successful?: true) } context 'async' do let(:processor_mock) { double('Processor', enqueue: true) } before do allow(LiveQA).to receive(:processor).and_return(processor_mock) LiveQA.set_group(42, { properties: { name: 'John Group' }}, no_ssl: true, space_name: 'test') end it { expect(processor_mock).to have_received(:enqueue).with(action: 'groups:update', id: 42, payload: hash_including(expected_arg), options: { space_name: 'test' }) } end context 'without async' do before do LiveQA.configurations.async = false allow(LiveQA::Group).to receive(:update).and_return(response) LiveQA.set_group(42, { properties: { name: 'John Group' }}, no_ssl: true, space_name: 'test') end it { expect(LiveQA::Group).to have_received(:update).with(42, hash_including(expected_arg), { no_ssl: true, space_name: 'test' }) } end context 'not enabled' do before do LiveQA.configurations.enabled = false LiveQA.set_group(42, { properties: { name: 'John Group' }}, no_ssl: true, space_name: 'test') end it { expect(LiveQA::Group).to_not receive(:update) } end end describe '.set_identity' do let(:expected_arg) {{ properties: { name: 'John Doe' }, message_id: kind_of(String), timestamp: kind_of(String), session_tracker_id: kind_of(String), }} let(:response) { double('LiveQA::Identity', successful?: true) } context 'async' do let(:processor_mock) { double('Processor', enqueue: true) } before do allow(LiveQA).to receive(:processor).and_return(processor_mock) LiveQA.set_identity(42, { properties: { name: 'John Doe' }}, no_ssl: true, space_name: 'test') end it { expect(processor_mock).to have_received(:enqueue).with(action: 'identities:update', id: 42, payload: hash_including(expected_arg), options: { space_name: 'test' }) } end context 'without async' do before do LiveQA.configurations.async = false allow(LiveQA::Identity).to receive(:update).and_return(response) LiveQA.set_identity(42, { properties: { name: 'John Doe' }}, no_ssl: true, space_name: 'test') end it { expect(LiveQA::Identity).to have_received(:update).with(42, hash_including(expected_arg), { no_ssl: true, space_name: 'test' }) } end context 'not enabled' do before do LiveQA.configurations.enabled = false LiveQA.set_identity(42, { properties: { name: 'John Doe' }}, no_ssl: true, space_name: 'test') end it { expect(LiveQA::Identity).to_not receive(:update) } end end describe '.watch' do let(:expected_arg) {{ template_flow: 'My Flow', expected_times: 42, message_id: kind_of(String), timestamp: kind_of(String), session_tracker_id: kind_of(String), }} let(:response) { double('LiveQA::Identity', successful?: true) } context 'async' do let(:processor_mock) { double('Processor', enqueue: true) } before do allow(LiveQA).to receive(:processor).and_return(processor_mock) LiveQA.watch('My Flow', { expected_times: 42 }, no_ssl: true, space_name: 'test') end it { expect(processor_mock).to have_received(:enqueue).with(action: 'watchers:create', payload: hash_including(expected_arg), options: { space_name: 'test' }) } end context 'without async' do before do LiveQA.configurations.async = false allow(LiveQA::Watcher).to receive(:create).and_return(response) LiveQA.watch('My Flow', { expected_times: 42 }, no_ssl: true, space_name: 'test') end it { expect(LiveQA::Watcher).to have_received(:create).with(hash_including(expected_arg), { no_ssl: true, space_name: 'test' }) } end context 'not enabled' do before do LiveQA.configurations.enabled = false LiveQA.watch('My Flow', { expected_times: 42 }, no_ssl: true, space_name: 'test') end it { expect(LiveQA::Watcher).to_not receive(:create) } end context 'without session' do let(:expected_arg) {{ template_flow: 'My Flow', expected_times: 42, message_id: kind_of(String), timestamp: kind_of(String), }} let(:processor_mock) { double('Processor', enqueue: true) } before do allow(LiveQA).to receive(:processor).and_return(processor_mock) LiveQA.watch('My Flow', { expected_times: 42, without_session: true }, no_ssl: true, space_name: 'test') end it { expect(processor_mock).to have_received(:enqueue).with(action: 'watchers:create', payload: hash_including(expected_arg), options: { space_name: 'test' }) } end end end