|
| 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 | + |
| 7 | +import WineApp from '../../src/components/wine-app'; |
| 8 | + |
| 9 | +const regions = ['Bordeaux', 'Bourgogne']; |
| 10 | + |
| 11 | +const wines1 = [ |
| 12 | + { |
| 13 | + "id": "chevrol-bel-air", |
| 14 | + "name": "Château Chevrol Bel Air", |
| 15 | + "type": "Rouge", |
| 16 | + "appellation": {"name": "Lalande-de-Pomerol", "region": "Bordeaux"}, |
| 17 | + "grapes": ["Cabernet Sauvignon", "Merlot", "Cabernet Franc"] |
| 18 | + }, |
| 19 | + { |
| 20 | + "id": "bel-air", |
| 21 | + "name": "Château Bel-Air", |
| 22 | + "type": "Rouge", |
| 23 | + "appellation": {"name": "Lussac-Saint-Emilion", "region": "Bordeaux"}, |
| 24 | + "grapes": ["Cabernet Sauvignon", "Merlot", "Cabernet Franc"] |
| 25 | + } |
| 26 | +]; |
| 27 | + |
| 28 | +const wines2 = [ |
| 29 | + { |
| 30 | + "id": "clarendelle", |
| 31 | + "name": "Clarendelle", |
| 32 | + "type": "Blanc", |
| 33 | + "appellation": {"name": "Bordeaux", "region": "Bordeaux"}, |
| 34 | + "grapes": ["Sauvignon", "Sémillon", "Muscadelle"] |
| 35 | + }, |
| 36 | + { |
| 37 | + "id": "les-hauts-de-tour-prignac", |
| 38 | + "name": "Les Hauts de Tour Prignac", |
| 39 | + "type": "Rouge", |
| 40 | + "appellation": {"name": "Médoc", "region": "Bordeaux"}, |
| 41 | + "grapes": ["Cabernet Sauvignon", "Merlot"] |
| 42 | + } |
| 43 | +]; |
| 44 | + |
| 45 | +function promise(data) { |
| 46 | + return { |
| 47 | + then(func) { |
| 48 | + return promise(func(data)); |
| 49 | + }, |
| 50 | + catch() {} |
| 51 | + }; |
| 52 | +} |
| 53 | + |
| 54 | +window.fetch = (url) => { |
| 55 | + if (url.startsWith('/api/regions')) { |
| 56 | + return promise({ json: () => regions }); |
| 57 | + } |
| 58 | + if (url.startsWith(`/api/wines?region=${regions[0]}`)) { |
| 59 | + return promise({ json: () => wines1 }); |
| 60 | + } |
| 61 | + if (url.startsWith(`/api/wines?region=${regions[1]}`)) { |
| 62 | + return promise({ json: () => wines2 }); |
| 63 | + } |
| 64 | + if (url.startsWith(`/api/wines/${wines1[0].id}`)) { |
| 65 | + return promise({ json: () => wines1[0] }); |
| 66 | + } |
| 67 | + if (url.startsWith(`/api/wines/${wines1[1].id}`)) { |
| 68 | + return promise({ json: () => wines1[1] }); |
| 69 | + } |
| 70 | + if (url.startsWith(`/api/wines/${wines2[0].id}`)) { |
| 71 | + return promise({ json: () => wines2[0] }); |
| 72 | + } |
| 73 | + if (url.startsWith(`/api/wines/${wines2[1].id}`)) { |
| 74 | + return promise({ json: () => wines2[1] }); |
| 75 | + } |
| 76 | + throw new Error(`Unknown URL : ${url}`); |
| 77 | +}; |
| 78 | +global.fetch = window.fetch; |
| 79 | + |
| 80 | +describe('<WineApp />', () => { |
| 81 | + it('doit afficher des régions', () => { |
| 82 | + const wrapper = mount( |
| 83 | + <WineApp /> |
| 84 | + ); |
| 85 | + |
| 86 | + const foundRegions = wrapper.find('div').filterWhere(n => regions.indexOf(n.get(0).innerHTML) > -1); |
| 87 | + expect(foundRegions.length).to.equal(2); |
| 88 | + }); |
| 89 | + it('doit afficher la liste des vins de la région', () => { |
| 90 | + const wrapper = mount( |
| 91 | + <WineApp /> |
| 92 | + ); |
| 93 | + |
| 94 | + const wineNames1 = Object.keys(wines1).map(k => wines1[k].name); |
| 95 | + const foundWines1 = wrapper.find('div').filterWhere(n => wineNames1.indexOf(n.get(0).innerHTML) > -1); |
| 96 | + expect(foundWines1.length).to.equal(3); // 3 because the first wine is showed |
| 97 | + |
| 98 | + const wineNames2 = Object.keys(wines2).map(k => wines2[k].name); |
| 99 | + const foundWines2 = wrapper.find('div').filterWhere(n => wineNames2.indexOf(n.get(0).innerHTML) > -1); |
| 100 | + expect(foundWines2.length).to.equal(0); |
| 101 | + }); |
| 102 | + it('doit afficher le premier vin de la région', () => { |
| 103 | + const wrapper = mount( |
| 104 | + <WineApp /> |
| 105 | + ); |
| 106 | + |
| 107 | + const firstWine = wines1[0]; |
| 108 | + const Wine = wrapper.find('Wine').at(0).first(); |
| 109 | + const foundName = Wine.find('div').filterWhere(n => n.get(0).innerHTML.indexOf(firstWine.name) > -1); |
| 110 | + const foundType = Wine.find('div').filterWhere(n => n.get(0).innerHTML.indexOf(firstWine.type) > -1); |
| 111 | + const foundRegion = Wine.find('div').filterWhere(n => n.get(0).innerHTML.indexOf(firstWine.appellation.region) > -1); |
| 112 | + const foundAppellation = Wine.find('div').filterWhere(n => n.get(0).innerHTML.indexOf(firstWine.appellation.name) > -1); |
| 113 | + const foundGrapes = Wine.find('div').filterWhere(n => n.get(0).innerHTML.indexOf(firstWine.grapes.join(', ')) > -1); |
| 114 | + |
| 115 | + expect(foundName.length).to.equal(2); // because of root div |
| 116 | + expect(foundType.length).to.equal(2); |
| 117 | + expect(foundAppellation.length).to.equal(2); |
| 118 | + expect(foundRegion.length).to.equal(2); |
| 119 | + expect(foundGrapes.length).to.equal(2); |
| 120 | + }); |
| 121 | + it('doit afficher des vins après avoir sélectionné une région', () => { |
| 122 | + const wrapper = mount( |
| 123 | + <WineApp /> |
| 124 | + ); |
| 125 | + |
| 126 | + { |
| 127 | + const wineNames1 = Object.keys(wines1).map(k => wines1[k].name); |
| 128 | + const foundWines1 = wrapper.find('div').filterWhere(n => wineNames1.indexOf(n.get(0).innerHTML) > -1); |
| 129 | + expect(foundWines1.length).to.equal(3); // 3 because the first wine is showed |
| 130 | + |
| 131 | + const wineNames2 = Object.keys(wines2).map(k => wines2[k].name); |
| 132 | + const foundWines2 = wrapper.find('div').filterWhere(n => wineNames2.indexOf(n.get(0).innerHTML) > -1); |
| 133 | + expect(foundWines2.length).to.equal(0); |
| 134 | + } |
| 135 | + |
| 136 | + const region2 = wrapper.find('div').filterWhere(n => n.get(0).innerHTML === regions[1]); |
| 137 | + region2.simulate('click'); |
| 138 | + |
| 139 | + { |
| 140 | + const wineNames1 = Object.keys(wines1).map(k => wines1[k].name); |
| 141 | + const foundWines1 = wrapper.find('div').filterWhere(n => wineNames1.indexOf(n.get(0).innerHTML) > -1); |
| 142 | + expect(foundWines1.length).to.equal(0); |
| 143 | + |
| 144 | + const wineNames2 = Object.keys(wines2).map(k => wines2[k].name); |
| 145 | + const foundWines2 = wrapper.find('div').filterWhere(n => wineNames2.indexOf(n.get(0).innerHTML) > -1); |
| 146 | + expect(foundWines2.length).to.equal(3); // 3 because the first wine is showed |
| 147 | + } |
| 148 | + }); |
| 149 | + it('doit afficher le premier vin de la région après avoir sélectionné une région', () => { |
| 150 | + const wrapper = mount( |
| 151 | + <WineApp /> |
| 152 | + ); |
| 153 | + |
| 154 | + { |
| 155 | + const firstWine = wines1[0]; |
| 156 | + const Wine = wrapper.find('Wine').at(0).first(); |
| 157 | + const foundName = Wine.find('div').filterWhere(n => n.get(0).innerHTML.indexOf(firstWine.name) > -1); |
| 158 | + const foundType = Wine.find('div').filterWhere(n => n.get(0).innerHTML.indexOf(firstWine.type) > -1); |
| 159 | + const foundRegion = Wine.find('div').filterWhere(n => n.get(0).innerHTML.indexOf(firstWine.appellation.region) > -1); |
| 160 | + const foundAppellation = Wine.find('div').filterWhere(n => n.get(0).innerHTML.indexOf(firstWine.appellation.name) > -1); |
| 161 | + const foundGrapes = Wine.find('div').filterWhere(n => n.get(0).innerHTML.indexOf(firstWine.grapes.join(', ')) > -1); |
| 162 | + |
| 163 | + expect(foundName.length).to.equal(2); // because of root div |
| 164 | + expect(foundType.length).to.equal(2); |
| 165 | + expect(foundAppellation.length).to.equal(2); |
| 166 | + expect(foundRegion.length).to.equal(2); |
| 167 | + expect(foundGrapes.length).to.equal(2); |
| 168 | + } |
| 169 | + |
| 170 | + const region2 = wrapper.find('div').filterWhere(n => n.get(0).innerHTML === regions[1]); |
| 171 | + region2.simulate('click'); |
| 172 | + |
| 173 | + { |
| 174 | + const firstWine = wines2[0]; |
| 175 | + const Wine = wrapper.find('Wine').at(0).first(); |
| 176 | + const foundName = Wine.find('div').filterWhere(n => n.get(0).innerHTML.indexOf(firstWine.name) > -1); |
| 177 | + const foundType = Wine.find('div').filterWhere(n => n.get(0).innerHTML.indexOf(firstWine.type) > -1); |
| 178 | + const foundRegion = Wine.find('div').filterWhere(n => n.get(0).innerHTML.indexOf(firstWine.appellation.region) > -1); |
| 179 | + const foundAppellation = Wine.find('div').filterWhere(n => n.get(0).innerHTML.indexOf(firstWine.appellation.name) > -1); |
| 180 | + const foundGrapes = Wine.find('div').filterWhere(n => n.get(0).innerHTML.indexOf(firstWine.grapes.join(', ')) > -1); |
| 181 | + |
| 182 | + expect(foundName.length).to.equal(2); // because of root div |
| 183 | + expect(foundType.length).to.equal(2); |
| 184 | + expect(foundAppellation.length).to.equal(3); |
| 185 | + expect(foundRegion.length).to.equal(3); |
| 186 | + expect(foundGrapes.length).to.equal(2); |
| 187 | + } |
| 188 | + }); |
| 189 | + it('doit afficher un vin après avoir sélectionné un nouveau vin', () => { |
| 190 | + const wrapper = mount( |
| 191 | + <WineApp /> |
| 192 | + ); |
| 193 | + |
| 194 | + { |
| 195 | + const firstWine = wines1[0]; |
| 196 | + const Wine = wrapper.find('Wine').at(0).first(); |
| 197 | + const foundName = Wine.find('div').filterWhere(n => n.get(0).innerHTML.indexOf(firstWine.name) > -1); |
| 198 | + const foundType = Wine.find('div').filterWhere(n => n.get(0).innerHTML.indexOf(firstWine.type) > -1); |
| 199 | + const foundRegion = Wine.find('div').filterWhere(n => n.get(0).innerHTML.indexOf(firstWine.appellation.region) > -1); |
| 200 | + const foundAppellation = Wine.find('div').filterWhere(n => n.get(0).innerHTML.indexOf(firstWine.appellation.name) > -1); |
| 201 | + const foundGrapes = Wine.find('div').filterWhere(n => n.get(0).innerHTML.indexOf(firstWine.grapes.join(', ')) > -1); |
| 202 | + |
| 203 | + expect(foundName.length).to.equal(2); // because of root div |
| 204 | + expect(foundType.length).to.equal(2); |
| 205 | + expect(foundAppellation.length).to.equal(2); |
| 206 | + expect(foundRegion.length).to.equal(2); |
| 207 | + expect(foundGrapes.length).to.equal(2); |
| 208 | + } |
| 209 | + |
| 210 | + const wine2 = wrapper.find('div').filterWhere(n => n.get(0).innerHTML === wines1[1].name); |
| 211 | + wine2.simulate('click'); |
| 212 | + |
| 213 | + { |
| 214 | + const firstWine = wines1[1]; |
| 215 | + const Wine = wrapper.find('Wine').at(0).first(); |
| 216 | + const foundName = Wine.find('div').filterWhere(n => n.get(0).innerHTML.indexOf(firstWine.name) > -1); |
| 217 | + const foundType = Wine.find('div').filterWhere(n => n.get(0).innerHTML.indexOf(firstWine.type) > -1); |
| 218 | + const foundRegion = Wine.find('div').filterWhere(n => n.get(0).innerHTML.indexOf(firstWine.appellation.region) > -1); |
| 219 | + const foundAppellation = Wine.find('div').filterWhere(n => n.get(0).innerHTML.indexOf(firstWine.appellation.name) > -1); |
| 220 | + const foundGrapes = Wine.find('div').filterWhere(n => n.get(0).innerHTML.indexOf(firstWine.grapes.join(', ')) > -1); |
| 221 | + |
| 222 | + expect(foundName.length).to.equal(2); // because of root div |
| 223 | + expect(foundType.length).to.equal(2); |
| 224 | + expect(foundAppellation.length).to.equal(2); |
| 225 | + expect(foundRegion.length).to.equal(2); |
| 226 | + expect(foundGrapes.length).to.equal(2); |
| 227 | + } |
| 228 | + }); |
| 229 | +}); |
0 commit comments