|
| 1 | +import { StationController } from './station.controller'; |
| 2 | + |
| 3 | +const mockFacade = { |
| 4 | + add: vi.fn(), |
| 5 | + findAll: vi.fn(), |
| 6 | + findById: vi.fn(), |
| 7 | + update: vi.fn(), |
| 8 | + remove: vi.fn(), |
| 9 | + removeAll: vi.fn(), |
| 10 | + restoreAll: vi.fn(), |
| 11 | +}; |
| 12 | + |
| 13 | +const makeSut = (facade = mockFacade) => { |
| 14 | + return new StationController(facade); |
| 15 | +}; |
| 16 | + |
| 17 | +describe('StationController', () => { |
| 18 | + describe('Add', () => { |
| 19 | + it('should add card', async () => { |
| 20 | + const sut = makeSut(); |
| 21 | + |
| 22 | + await sut.add('any_name', 'any_line'); |
| 23 | + |
| 24 | + expect(mockFacade.add).toHaveBeenCalledWith({ |
| 25 | + name: 'any_name', |
| 26 | + line: 'any_line', |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + it('should throw when use case throws', async () => { |
| 31 | + const facade = { |
| 32 | + add: vi.fn().mockRejectedValueOnce(new Error()), |
| 33 | + findAll: vi.fn(), |
| 34 | + findById: vi.fn(), |
| 35 | + update: vi.fn(), |
| 36 | + remove: vi.fn(), |
| 37 | + removeAll: vi.fn(), |
| 38 | + restoreAll: vi.fn(), |
| 39 | + }; |
| 40 | + |
| 41 | + const sut = makeSut(facade); |
| 42 | + |
| 43 | + await expect( |
| 44 | + async () => await sut.add('any_name', 'any_line'), |
| 45 | + ).rejects.toThrow(); |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + describe('FindAll', () => { |
| 50 | + it('should find all stations', async () => { |
| 51 | + const sut = makeSut(); |
| 52 | + |
| 53 | + await sut.findAll(); |
| 54 | + |
| 55 | + expect(mockFacade.findAll).toHaveBeenCalled(); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should throw when use case throws', async () => { |
| 59 | + const facade = { |
| 60 | + add: vi.fn(), |
| 61 | + findAll: vi.fn().mockRejectedValueOnce(new Error()), |
| 62 | + findById: vi.fn(), |
| 63 | + update: vi.fn(), |
| 64 | + remove: vi.fn(), |
| 65 | + removeAll: vi.fn(), |
| 66 | + restoreAll: vi.fn(), |
| 67 | + }; |
| 68 | + |
| 69 | + const sut = makeSut(facade); |
| 70 | + |
| 71 | + await expect(async () => await sut.findAll()).rejects.toThrow(); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + describe('FindById', () => { |
| 76 | + it('should find station by id', async () => { |
| 77 | + const sut = makeSut(); |
| 78 | + |
| 79 | + await sut.findById(1); |
| 80 | + |
| 81 | + expect(mockFacade.findById).toHaveBeenCalledWith({ |
| 82 | + id: 1, |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + it('should throw when use case throws', async () => { |
| 87 | + const facade = { |
| 88 | + add: vi.fn(), |
| 89 | + findAll: vi.fn(), |
| 90 | + findById: vi.fn().mockRejectedValueOnce(new Error()), |
| 91 | + update: vi.fn(), |
| 92 | + remove: vi.fn(), |
| 93 | + removeAll: vi.fn(), |
| 94 | + restoreAll: vi.fn(), |
| 95 | + }; |
| 96 | + |
| 97 | + const sut = makeSut(facade); |
| 98 | + |
| 99 | + await expect(async () => await sut.findById(1)).rejects.toThrow(); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + describe('Update', () => { |
| 104 | + it('should update card', async () => { |
| 105 | + const sut = makeSut(); |
| 106 | + |
| 107 | + await sut.update(1, 'any_name', 'any_line'); |
| 108 | + |
| 109 | + expect(mockFacade.update).toHaveBeenCalledWith({ |
| 110 | + id: 1, |
| 111 | + name: 'any_name', |
| 112 | + line: 'any_line', |
| 113 | + }); |
| 114 | + }); |
| 115 | + |
| 116 | + it('should throw when use case throws', async () => { |
| 117 | + const facade = { |
| 118 | + add: vi.fn(), |
| 119 | + findAll: vi.fn(), |
| 120 | + findById: vi.fn(), |
| 121 | + update: vi.fn().mockRejectedValueOnce(new Error()), |
| 122 | + remove: vi.fn(), |
| 123 | + removeAll: vi.fn(), |
| 124 | + restoreAll: vi.fn(), |
| 125 | + }; |
| 126 | + |
| 127 | + const sut = makeSut(facade); |
| 128 | + |
| 129 | + await expect( |
| 130 | + async () => await sut.update(1, 'any_name', 'any_line'), |
| 131 | + ).rejects.toThrow(); |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + describe('Remove', () => { |
| 136 | + it('should remove card', async () => { |
| 137 | + const sut = makeSut(); |
| 138 | + |
| 139 | + await sut.remove(1); |
| 140 | + |
| 141 | + expect(mockFacade.remove).toHaveBeenCalledWith({ |
| 142 | + id: 1, |
| 143 | + }); |
| 144 | + }); |
| 145 | + |
| 146 | + it('should throw when use case throws', async () => { |
| 147 | + const facade = { |
| 148 | + add: vi.fn(), |
| 149 | + findAll: vi.fn(), |
| 150 | + findById: vi.fn(), |
| 151 | + update: vi.fn(), |
| 152 | + remove: vi.fn().mockRejectedValueOnce(new Error()), |
| 153 | + removeAll: vi.fn(), |
| 154 | + restoreAll: vi.fn(), |
| 155 | + }; |
| 156 | + |
| 157 | + const sut = makeSut(facade); |
| 158 | + |
| 159 | + await expect(async () => await sut.remove(1)).rejects.toThrow(); |
| 160 | + }); |
| 161 | + }); |
| 162 | + |
| 163 | + describe('RemoveAll', () => { |
| 164 | + it('should remove all cards', async () => { |
| 165 | + const sut = makeSut(); |
| 166 | + |
| 167 | + await sut.removeAll(); |
| 168 | + |
| 169 | + expect(mockFacade.removeAll).toHaveBeenCalled(); |
| 170 | + }); |
| 171 | + |
| 172 | + it('should throw when use case throws', async () => { |
| 173 | + const facade = { |
| 174 | + add: vi.fn(), |
| 175 | + findAll: vi.fn(), |
| 176 | + findById: vi.fn(), |
| 177 | + update: vi.fn(), |
| 178 | + remove: vi.fn(), |
| 179 | + removeAll: vi.fn().mockRejectedValueOnce(new Error()), |
| 180 | + restoreAll: vi.fn(), |
| 181 | + }; |
| 182 | + |
| 183 | + const sut = makeSut(facade); |
| 184 | + |
| 185 | + await expect(async () => await sut.removeAll()).rejects.toThrow(); |
| 186 | + }); |
| 187 | + }); |
| 188 | + |
| 189 | + describe('RestoreAll', () => { |
| 190 | + it('should restore all cards', async () => { |
| 191 | + const sut = makeSut(); |
| 192 | + |
| 193 | + await sut.restoreAll(); |
| 194 | + |
| 195 | + expect(mockFacade.restoreAll).toHaveBeenCalled(); |
| 196 | + }); |
| 197 | + |
| 198 | + it('should throw when use case throws', async () => { |
| 199 | + const facade = { |
| 200 | + add: vi.fn(), |
| 201 | + findAll: vi.fn(), |
| 202 | + findById: vi.fn(), |
| 203 | + update: vi.fn(), |
| 204 | + remove: vi.fn(), |
| 205 | + removeAll: vi.fn(), |
| 206 | + restoreAll: vi.fn().mockRejectedValueOnce(new Error()), |
| 207 | + }; |
| 208 | + |
| 209 | + const sut = makeSut(facade); |
| 210 | + |
| 211 | + await expect(async () => await sut.restoreAll()).rejects.toThrow(); |
| 212 | + }); |
| 213 | + }); |
| 214 | +}); |
0 commit comments