Skip to content

Commit 328de3c

Browse files
committed
Change fixtures to better match something and add extended tests for person helper
1 parent 8488002 commit 328de3c

File tree

2 files changed

+53
-17
lines changed

2 files changed

+53
-17
lines changed

spec/fixtures/files/json/all_ptime_employees.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"graduation" : "MSc in writing",
3434
"department_shortname" : "SYS",
3535
"employment_roles" : [],
36-
"is_employed" : false,
36+
"is_employed" : true,
3737
"birthdate" : "01.04.2001",
3838
"location" : "Bern"
3939
}

spec/helpers/person_helper_spec.rb

Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,68 @@
11
require 'rails_helper'
22
RSpec.describe PersonHelper, type: :helper do
33

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+
420
describe '#fetch_people_data' do
521

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
735
allow(Skills).to receive(:use_ptime_sync?).and_return(true)
836
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)
1438
end
1539

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
1741
allow(Skills).to receive(:use_ptime_sync?).and_return(false)
1842

1943
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 } }
2860
]
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")
3066
end
3167
end
3268
end

0 commit comments

Comments
 (0)