|
| 1 | +/* eslint no-undef:0, no-unused-vars:1, react/jsx-closing-bracket-location: 0, react/jsx-max-props-per-line: 0 */ |
| 2 | + |
| 3 | +import React from 'react'; |
| 4 | +import { expect } from 'chai'; |
| 5 | +import { mount } from 'enzyme'; |
| 6 | +import { createMemoryHistory } from 'react-router'; |
| 7 | + |
| 8 | +import { App } from '../../src/app'; |
| 9 | + |
| 10 | +const regions = ['Bordeaux', 'Bourgogne']; |
| 11 | + |
| 12 | +const wines1 = [ |
| 13 | + { |
| 14 | + "id": "chevrol-bel-air", |
| 15 | + "name": "Château Chevrol Bel Air", |
| 16 | + "type": "Rouge", |
| 17 | + "appellation": {"name": "Lalande-de-Pomerol", "region": "Bordeaux"}, |
| 18 | + "grapes": ["Cabernet Sauvignon", "Merlot", "Cabernet Franc"] |
| 19 | + }, |
| 20 | + { |
| 21 | + "id": "bel-air", |
| 22 | + "name": "Château Bel-Air", |
| 23 | + "type": "Rouge", |
| 24 | + "appellation": {"name": "Lussac-Saint-Emilion", "region": "Bordeaux"}, |
| 25 | + "grapes": ["Cabernet Sauvignon", "Merlot", "Cabernet Franc"] |
| 26 | + } |
| 27 | +]; |
| 28 | + |
| 29 | +const wines2 = [ |
| 30 | + { |
| 31 | + "id": "clarendelle", |
| 32 | + "name": "Clarendelle", |
| 33 | + "type": "Blanc", |
| 34 | + "appellation": {"name": "Bordeaux", "region": "Bordeaux"}, |
| 35 | + "grapes": ["Sauvignon", "Sémillon", "Muscadelle"] |
| 36 | + }, |
| 37 | + { |
| 38 | + "id": "les-hauts-de-tour-prignac", |
| 39 | + "name": "Les Hauts de Tour Prignac", |
| 40 | + "type": "Rouge", |
| 41 | + "appellation": {"name": "Médoc", "region": "Bordeaux"}, |
| 42 | + "grapes": ["Cabernet Sauvignon", "Merlot"] |
| 43 | + } |
| 44 | +]; |
| 45 | + |
| 46 | +function promise(data) { |
| 47 | + return { |
| 48 | + then(func) { |
| 49 | + return promise(func(data)); |
| 50 | + }, |
| 51 | + catch() {} |
| 52 | + }; |
| 53 | +} |
| 54 | + |
| 55 | +window.fetch = (url) => { |
| 56 | + if (url.startsWith('/api/regions')) { |
| 57 | + return promise({ json: () => regions }); |
| 58 | + } |
| 59 | + if (url.startsWith(`/api/wines?region=${regions[0]}`)) { |
| 60 | + return promise({ json: () => wines1 }); |
| 61 | + } |
| 62 | + if (url.startsWith(`/api/wines?region=${regions[1]}`)) { |
| 63 | + return promise({ json: () => wines2 }); |
| 64 | + } |
| 65 | + if (url.startsWith(`/api/wines/${wines1[0].id}`)) { |
| 66 | + return promise({ json: () => wines1[0] }); |
| 67 | + } |
| 68 | + if (url.startsWith(`/api/wines/${wines1[1].id}`)) { |
| 69 | + return promise({ json: () => wines1[1] }); |
| 70 | + } |
| 71 | + if (url.startsWith(`/api/wines/${wines2[0].id}`)) { |
| 72 | + return promise({ json: () => wines2[0] }); |
| 73 | + } |
| 74 | + if (url.startsWith(`/api/wines/${wines2[1].id}`)) { |
| 75 | + return promise({ json: () => wines2[1] }); |
| 76 | + } |
| 77 | + throw new Error(`Unknown URL : ${url}`); |
| 78 | +}; |
| 79 | +global.fetch = window.fetch; |
| 80 | + |
| 81 | +describe('<App />', () => { |
| 82 | + it('doit afficher des régions', () => { |
| 83 | + const history = createMemoryHistory(window.location); |
| 84 | + const wrapper = mount( |
| 85 | + <App history={history} /> |
| 86 | + ); |
| 87 | + const foundRegions = wrapper.find('Regions').find('div').filterWhere(n => regions.indexOf(n.get(0).innerHTML) > -1); |
| 88 | + expect(foundRegions.length).to.equal(2); |
| 89 | + }); |
| 90 | + |
| 91 | + it('doit afficher des vins après avoir sélectionné une région', () => { |
| 92 | + const history = createMemoryHistory(window.location); |
| 93 | + const wrapper = mount( |
| 94 | + <App history={history} /> |
| 95 | + ); |
| 96 | + |
| 97 | + { |
| 98 | + const foundRegions = wrapper.find('Regions').find('div').filterWhere(n => regions.indexOf(n.get(0).innerHTML) > -1); |
| 99 | + expect(foundRegions.length).to.equal(2); |
| 100 | + } |
| 101 | + |
| 102 | + const region2 = wrapper.find('Regions').find('div').filterWhere(n => n.get(0).innerHTML === regions[1]); |
| 103 | + region2.simulate('click'); |
| 104 | + |
| 105 | + { |
| 106 | + const wineNames1 = Object.keys(wines1).map(k => wines1[k].name); |
| 107 | + const foundWines1 = wrapper.find('WineList').find('div').filterWhere(n => wineNames1.indexOf(n.get(0).innerHTML) > -1); |
| 108 | + expect(foundWines1.length).to.equal(0); |
| 109 | + |
| 110 | + const wineNames2 = Object.keys(wines2).map(k => wines2[k].name); |
| 111 | + const foundWines2 = wrapper.find('WineList').find('div').filterWhere(n => wineNames2.indexOf(n.get(0).innerHTML) > -1); |
| 112 | + expect(foundWines2.length).to.equal(2); |
| 113 | + } |
| 114 | + |
| 115 | + history.goBack(); |
| 116 | + |
| 117 | + const region1 = wrapper.find('Regions').find('div').filterWhere(n => n.get(0).innerHTML === regions[0]); |
| 118 | + region1.simulate('click'); |
| 119 | + |
| 120 | + { |
| 121 | + const wineNames1 = Object.keys(wines1).map(k => wines1[k].name); |
| 122 | + const foundWines1 = wrapper.find('WineList').find('div').filterWhere(n => wineNames1.indexOf(n.get(0).innerHTML) > -1); |
| 123 | + expect(foundWines1.length).to.equal(2); |
| 124 | + |
| 125 | + const wineNames2 = Object.keys(wines2).map(k => wines2[k].name); |
| 126 | + const foundWines2 = wrapper.find('WineList').find('div').filterWhere(n => wineNames2.indexOf(n.get(0).innerHTML) > -1); |
| 127 | + expect(foundWines2.length).to.equal(0); |
| 128 | + } |
| 129 | + |
| 130 | + }); |
| 131 | + |
| 132 | + it('doit afficher un vin après avoir sélectionné un nouveau vin', () => { |
| 133 | + const history = createMemoryHistory(window.location); |
| 134 | + const wrapper = mount( |
| 135 | + <App history={history} /> |
| 136 | + ); |
| 137 | + |
| 138 | + const region1 = wrapper.find('Regions').find('div').filterWhere(n => n.get(0).innerHTML === regions[0]); |
| 139 | + region1.simulate('click'); |
| 140 | + |
| 141 | + const wine1 = wrapper.find('WineList').find('div').filterWhere(n => n.get(0).innerHTML === wines1[0].name); |
| 142 | + wine1.simulate('click'); |
| 143 | + |
| 144 | + { |
| 145 | + const firstWine = wines1[0]; |
| 146 | + const Wine = wrapper.find('Wine').at(0).first(); |
| 147 | + const foundName = Wine.find('div').filterWhere(n => n.get(0).innerHTML.indexOf(firstWine.name) > -1); |
| 148 | + const foundType = Wine.find('div').filterWhere(n => n.get(0).innerHTML.indexOf(firstWine.type) > -1); |
| 149 | + const foundRegion = Wine.find('div').filterWhere(n => n.get(0).innerHTML.indexOf(firstWine.appellation.region) > -1); |
| 150 | + const foundAppellation = Wine.find('div').filterWhere(n => n.get(0).innerHTML.indexOf(firstWine.appellation.name) > -1); |
| 151 | + const foundGrapes = Wine.find('div').filterWhere(n => n.get(0).innerHTML.indexOf(firstWine.grapes.join(', ')) > -1); |
| 152 | + |
| 153 | + expect(foundName.length).to.equal(2); // because of root div |
| 154 | + expect(foundType.length).to.equal(2); |
| 155 | + expect(foundAppellation.length).to.equal(2); |
| 156 | + expect(foundRegion.length).to.equal(2); |
| 157 | + expect(foundGrapes.length).to.equal(2); |
| 158 | + } |
| 159 | + |
| 160 | + history.goBack(); |
| 161 | + |
| 162 | + const wine2 = wrapper.find('WineList').find('div').filterWhere(n => n.get(0).innerHTML === wines1[1].name); |
| 163 | + wine2.simulate('click'); |
| 164 | + |
| 165 | + { |
| 166 | + const firstWine = wines1[1]; |
| 167 | + const Wine = wrapper.find('Wine').at(0).first(); |
| 168 | + const foundName = Wine.find('div').filterWhere(n => n.get(0).innerHTML.indexOf(firstWine.name) > -1); |
| 169 | + const foundType = Wine.find('div').filterWhere(n => n.get(0).innerHTML.indexOf(firstWine.type) > -1); |
| 170 | + const foundRegion = Wine.find('div').filterWhere(n => n.get(0).innerHTML.indexOf(firstWine.appellation.region) > -1); |
| 171 | + const foundAppellation = Wine.find('div').filterWhere(n => n.get(0).innerHTML.indexOf(firstWine.appellation.name) > -1); |
| 172 | + const foundGrapes = Wine.find('div').filterWhere(n => n.get(0).innerHTML.indexOf(firstWine.grapes.join(', ')) > -1); |
| 173 | + |
| 174 | + expect(foundName.length).to.equal(2); // because of root div |
| 175 | + expect(foundType.length).to.equal(2); |
| 176 | + expect(foundAppellation.length).to.equal(2); |
| 177 | + expect(foundRegion.length).to.equal(2); |
| 178 | + expect(foundGrapes.length).to.equal(2); |
| 179 | + } |
| 180 | + }); |
| 181 | +}); |
0 commit comments