|
1 | 1 | require 'rails_helper'
|
2 | 2 |
|
3 | 3 | describe DeputadosController, type: :controller do
|
4 |
| - let(:deputado) { Deputado.create! FactoryGirl.attributes_for(:deputado) } |
| 4 | + before do |
| 5 | + create(:despesa, valor_liquido: 10, valor_documento: 100, deputado_id: deputy.id) |
| 6 | + create(:despesa, valor_liquido: 12, valor_documento: 120, deputado_id: deputy.id) |
| 7 | + |
| 8 | + create(:despesa, valor_liquido: 13, valor_documento: 130, deputado_id: john_doe.id) |
| 9 | + |
| 10 | + DeputadosIndex.purge! |
| 11 | + DeputadosIndex.import |
| 12 | + end |
| 13 | + |
| 14 | + let(:deputy) { create(:deputado, nome: "Deputado") } |
| 15 | + let(:john_doe) { create(:john_doe) } |
5 | 16 |
|
6 | 17 | render_views
|
7 | 18 |
|
8 | 19 | describe 'GET #index' do
|
9 |
| - context 'via json' do |
10 |
| - it 'deve listar os deputados e exibe os atributos corretamente' do |
11 |
| - get :index, params: {q: deputado.nome}, format: :json |
| 20 | + context 'when using json' do |
| 21 | + it 'should list deputies' do |
| 22 | + get :index, q: "Deputado", format: :json |
12 | 23 |
|
13 | 24 | json = JSON.parse(response.body)
|
14 | 25 |
|
15 |
| - amostra = json.first |
16 |
| - espero_que(amostra).tenha %w(id nome email partido uf url_foto total_despesas total_votos |
17 |
| - porcentagem_votos situacao_candidatura) |
| 26 | + expect(json.size).to be(1) |
18 | 27 | end
|
19 |
| - end |
20 | 28 |
|
21 |
| - context 'via html' do |
22 |
| - before(:each) { get :index, params: {q: deputado.nome}} |
| 29 | + it 'should not list non-matching deputy' do |
| 30 | + get :index, q: "Deputado", format: :json |
23 | 31 |
|
24 |
| - it 'deve exibir total de votos' do |
25 |
| - expect(response.body).to have_content('votos') |
| 32 | + expect(response.body).not_to match("John Doe") |
26 | 33 | end
|
27 | 34 |
|
28 |
| - it 'deve exibir link Início' do |
29 |
| - expect(response.body).to have_link('Início') |
| 35 | + context 'should correct return attributes for matching deputies' do |
| 36 | + before do |
| 37 | + get :index, q: deputy.nome, format: :json |
| 38 | + end |
| 39 | + |
| 40 | + it { expect(response.body).to match(%r["id":#{deputy.id}]) } |
| 41 | + it { expect(response.body).to match(%r["total_votos":#{deputy.total_votos}]) } |
| 42 | + it { expect(response.body).to match(%r["porcentagem_votos":#{deputy.porcentagem_votos}]) } |
| 43 | + it { expect(response.body).to match(%r["total_despesas":"R\$ 22,00"]) } |
| 44 | + |
| 45 | + %w(nome email partido uf url_foto situacao_candidatura).each do |attribute| |
| 46 | + it { expect(response.body).to match(%r["#{attribute}":"#{deputy.send(attribute)}"]) } |
| 47 | + end |
30 | 48 | end
|
31 | 49 |
|
32 |
| - it 'deve exibir campo de busca no rodapé' do |
33 |
| - expect(response.body).to have_selector('footer .search') |
| 50 | + %w[nome nome_parlamentar partido matricula email uf].each do |param| |
| 51 | + context "should search using #{param} attribute from deputy" do |
| 52 | + it 'and returns correct deputy' do |
| 53 | + get :index, q: deputy.send(param), format: :json |
| 54 | + |
| 55 | + expect(response.body).to match deputy.nome |
| 56 | + end |
| 57 | + |
| 58 | + it 'and does not return incorrect deputy' do |
| 59 | + get :index, q: deputy.send(param), format: :json |
| 60 | + |
| 61 | + expect(response.body).not_to match 'John Doe' |
| 62 | + end |
| 63 | + end |
34 | 64 | end
|
35 | 65 | end
|
36 | 66 |
|
37 |
| - context 'busca' do |
38 |
| - it 'deve retornar resultado para cada parametro' do |
39 |
| - [:nome, :nome_parlamentar, :partido, :matricula, :url_foto, :email, :uf].each do |attr| |
40 |
| - get :index, params: {q: deputado[attr]}, format: :json |
41 |
| - expect(response.body).to_not be_blank |
42 |
| - end |
| 67 | + context 'when using html' do |
| 68 | + before do |
| 69 | + get :index, q: deputy.nome |
43 | 70 | end
|
44 | 71 |
|
45 |
| - it 'nao deve exibir logo no cover' do |
46 |
| - get :index, params: {q: deputado.nome} |
47 |
| - expect(response.body).to_not have_selector('#logo') |
| 72 | + it { expect(response.body).to have_content(deputy.nome_parlamentar) } |
| 73 | + it { expect(response.body).to have_content('Partido: PDSK') } |
| 74 | + it { expect(response.body).to have_content('Estado: KD') } |
| 75 | + it { expect(response.body).to have_content('Total de votos: 123') } |
| 76 | + it { expect(response.body).to have_content('Despesas: R$ 22,00') } |
| 77 | + |
| 78 | + it 'should not show info about non-matching deputy' do |
| 79 | + expect(response.body).not_to have_content('John Doe') |
48 | 80 | end
|
49 | 81 | end
|
50 | 82 | end
|
51 | 83 |
|
52 |
| - |
53 | 84 | describe 'GET #show' do
|
54 |
| - before(:each) { deputado.despesas << FactoryGirl.build(:despesa) } |
| 85 | + context 'when using json' do |
| 86 | + context 'should return correct information' do |
| 87 | + before { get :show, id: deputy.id, format: :json } |
55 | 88 |
|
56 |
| - context 'via json' do |
57 |
| - it 'deve exibir o deputado e os atributos corretamente' do |
58 |
| - get :show, id: deputado.id, format: :json |
59 |
| - |
60 |
| - json = JSON.parse(response.body) |
61 |
| - |
62 |
| - amostra_despesa = json.first |
63 |
| - espero_que(amostra_despesa).tenha %w(tipo total total_liquido) |
| 89 | + it { expect(response.body).to match %q[tipo":"descricao] } |
| 90 | + it { expect(response.body).to match %q[total":"R\$ 220,00] } |
| 91 | + it { expect(response.body).to match %q[total_liquido":"R\$ 22,00] } |
64 | 92 | end
|
65 | 93 | end
|
66 | 94 |
|
67 |
| - context 'via html' do |
68 |
| - it 'deve exibir o total de votos, votos validos e situacao' do |
69 |
| - get :show, id: deputado.id |
| 95 | + context 'when using html' do |
| 96 | + before { get :show, id: deputy.id } |
| 97 | + |
| 98 | + context 'should return correct infomation' do |
| 99 | + it { expect(response.body).to have_content('123 votos') } |
| 100 | + it { expect(response.body).to have_content('10,00% votos válidos') } |
| 101 | + it { expect(response.body).to have_content('Eleito') } |
| 102 | + end |
70 | 103 |
|
71 |
| - expect(response.body).to have_content('votos') |
72 |
| - expect(response.body).to have_content('votos válidos') |
73 |
| - expect(response.body).to have_content(deputado.situacao_candidatura) |
| 104 | + context 'should correct return @opengraph' do |
| 105 | + it { expect(assigns(:opengraph)).to include(title: "Deputado: Deputado") } |
| 106 | + it { expect(assigns(:opengraph)).to include(type: "website") } |
| 107 | + it { expect(assigns(:opengraph)).to include(url: "http://test.host/deputados/#{deputy.id}") } |
| 108 | + it { expect(assigns(:opengraph)).to include(image: "http://url.com/foto") } |
| 109 | + it { expect(assigns(:opengraph)).to include(site_name: "Peba") } |
74 | 110 | end
|
75 | 111 | end
|
76 | 112 | end
|
|
0 commit comments