|
1 | 1 | require 'rails_helper' |
2 | 2 | RSpec.describe PersonHelper, type: :helper do |
3 | 3 |
|
| 4 | + local_data = [ |
| 5 | + ["Bob Anderson", "/people/902541635", { class: "p-0", "data-html": "<a href='/people/902541635' class='dropdown-option-link'>Bob Anderson</a>" }], |
| 6 | + ["Alice Mante", "/people/663665735", { class: "p-0", "data-html": "<a href='/people/663665735' class='dropdown-option-link'>Alice Mante</a>" }], |
| 7 | + ["ken", "/people/155397742", { class: "p-0", "data-html": "<a href='/people/155397742' class='dropdown-option-link'>ken</a>" }], |
| 8 | + ["Charlie Ford", "/people/786122151", { class: "p-0", "data-html": "<a href='/people/786122151' class='dropdown-option-link'>Charlie Ford</a>" }], |
| 9 | + ["Wally Allround", "/people/790004949", { class: "p-0", "data-html": "<a href='/people/790004949' class='dropdown-option-link'>Wally Allround</a>" }], |
| 10 | + ["Hope Sunday", "/people/247095502", { class: "p-0", "data-html": "<a href='/people/247095502' class='dropdown-option-link'>Hope Sunday</a>" }], |
| 11 | + ["Longmax Smith", "/people/169654640", { class: "p-0", "data-html": "<a href='/people/169654640' class='dropdown-option-link'>Longmax Smith</a>" }] |
| 12 | + ] |
| 13 | + |
| 14 | + ptime_data = [ |
| 15 | + ["Longmax Smith", "/people/new?ptime_employee_id=33", { class: "p-0", "data-html": "<a href='/people/new?ptime_employee_id=33' class='dropdown-option-link'>Longmax Smith</a>" }], |
| 16 | + ["Alice Mante", "/people/new?ptime_employee_id=21", { class: "p-0", "data-html": "<a href='/people/new?ptime_employee_id=21' class='dropdown-option-link'>Alice Mante</a>" }], |
| 17 | + ["Charlie Ford", "/people/new?ptime_employee_id=45", { class: "p-0", "data-html": "<a href='/people/new?ptime_employee_id=45' class='dropdown-option-link'>Charlie Ford</a>" }] |
| 18 | + ] |
| 19 | + |
4 | 20 | describe '#fetch_people_data' do |
5 | 21 |
|
6 | | - it 'should send request to ptime and return employed employees when ptime_sync is active' do |
| 22 | + it 'should return people sorted alphabetically, ignoring case' do |
| 23 | + allow(Skills).to receive(:use_ptime_sync?).and_return(false) |
| 24 | + allow(helper).to receive(:fetch_local_people_data).and_return([ |
| 25 | + ["bob", "/people/1", {}], |
| 26 | + ["Alice", "/people/2", {}], |
| 27 | + ["charlie", "/people/3", {}] |
| 28 | + ]) |
| 29 | + |
| 30 | + sorted = helper.sorted_people.map(&:first) |
| 31 | + expect(sorted).to eq(["Alice", "bob", "charlie"]) |
| 32 | + end |
| 33 | + |
| 34 | + it 'should return ptime data when ptime_sync is active' do |
7 | 35 | allow(Skills).to receive(:use_ptime_sync?).and_return(true) |
8 | 36 | skills_people = helper.fetch_people_data |
9 | | - expected_people = [ |
10 | | - ["Longmax Smith", "/people/new?ptime_employee_id=33", { class: "p-0", "data-html": "<a href='/people/new?ptime_employee_id=33' class='dropdown-option-link'>Longmax Smith</a>" }], |
11 | | - ["Charlie Ford", "/people/new?ptime_employee_id=45", { class: "p-0", "data-html": "<a href='/people/new?ptime_employee_id=45' class='dropdown-option-link'>Charlie Ford</a>" }] |
12 | | - ] |
13 | | - expect(skills_people).to match_array(expected_people) |
| 37 | + expect(skills_people).to match_array(ptime_data) |
14 | 38 | end |
15 | 39 |
|
16 | | - it 'should return people from skills database when ptime_sync is inactive' do |
| 40 | + it 'should return local data when ptime_sync is inactive' do |
17 | 41 | allow(Skills).to receive(:use_ptime_sync?).and_return(false) |
18 | 42 |
|
19 | 43 | skills_people = helper.fetch_people_data |
20 | | - expected = [ |
21 | | - ["Bob Anderson", "/people/902541635", { class: "p-0", "data-html": "<a href='/people/902541635' class='dropdown-option-link'>Bob Anderson</a>" }], |
22 | | - ["Alice Mante", "/people/663665735", { class: "p-0", "data-html": "<a href='/people/663665735' class='dropdown-option-link'>Alice Mante</a>" }], |
23 | | - ["ken", "/people/155397742", { class: "p-0", "data-html": "<a href='/people/155397742' class='dropdown-option-link'>ken</a>" }], |
24 | | - ["Charlie Ford", "/people/786122151", { class: "p-0", "data-html": "<a href='/people/786122151' class='dropdown-option-link'>Charlie Ford</a>" }], |
25 | | - ["Wally Allround", "/people/790004949", { class: "p-0", "data-html": "<a href='/people/790004949' class='dropdown-option-link'>Wally Allround</a>" }], |
26 | | - ["Hope Sunday", "/people/247095502", { class: "p-0", "data-html": "<a href='/people/247095502' class='dropdown-option-link'>Hope Sunday</a>" }], |
27 | | - ["Longmax Smith", "/people/169654640", { class: "p-0", "data-html": "<a href='/people/169654640' class='dropdown-option-link'>Longmax Smith</a>" }] |
| 44 | + expect(skills_people).to eq(local_data) |
| 45 | + end |
| 46 | + |
| 47 | + it 'should fall back to local data when PTime raises an error' do |
| 48 | + allow(Skills).to receive(:use_ptime_sync?).and_return(true) |
| 49 | + allow(helper).to receive(:fetch_ptime_people_data).and_raise(CustomExceptions::PTimeClientError) |
| 50 | + |
| 51 | + people = helper.fetch_people_data |
| 52 | + expect(people).to eq(local_data) |
| 53 | + end |
| 54 | + |
| 55 | + it 'should exclude unemployed employees from the dropdown' do |
| 56 | + allow(Skills).to receive(:use_ptime_sync?).and_return(true) |
| 57 | + ptime_employees = [ |
| 58 | + { id: 1, attributes: { firstname: "John", lastname: "Doe", is_employed: true } }, |
| 59 | + { id: 2, attributes: { firstname: "Jane", lastname: "Smith", is_employed: false } } |
28 | 60 | ] |
29 | | - expect(skills_people).to eq(expected) |
| 61 | + allow_any_instance_of(Ptime::Client).to receive(:request).and_return(ptime_employees) |
| 62 | + |
| 63 | + people = helper.fetch_people_data |
| 64 | + expect(people.map(&:first)).to include("John Doe") |
| 65 | + expect(people.map(&:first)).not_to include("Jane Smith") |
30 | 66 | end |
31 | 67 | end |
32 | 68 | end |
0 commit comments