|
| 1 | +import { |
| 2 | + HttpClientTestingModule, |
| 3 | + HttpTestingController |
| 4 | +} from '@angular/common/http/testing'; |
| 5 | +import { TestBed } from "@angular/core/testing"; |
| 6 | +import { getFeedMock, getFeedsMock } from '@mocks/feed.mock'; |
| 7 | +import { getUserMock } from '@mocks/user.mock'; |
| 8 | +import { environment } from 'environments/environment'; |
| 9 | + |
| 10 | +import { Feed } from '@models/feed.model'; |
| 11 | +import { User } from '@models/user.model'; |
| 12 | + |
| 13 | +import { AuthService } from './auth.service'; |
| 14 | +import { FeedService } from "./feed.service"; |
| 15 | + |
| 16 | +const { base_url } = environment; |
| 17 | + |
| 18 | +describe('Feed Service', () => { |
| 19 | + let feedService: FeedService; |
| 20 | + let httpController: HttpTestingController; |
| 21 | + let spyAuthService: jasmine.SpyObj<AuthService>; |
| 22 | + |
| 23 | + beforeEach(() => { |
| 24 | + const spyAuth = jasmine.createSpyObj( |
| 25 | + 'AuthService', |
| 26 | + ['getUserActive'], |
| 27 | + { userActive: { email: 'example@test.com', savedFeeds: ['1234'] } |
| 28 | + }); |
| 29 | + TestBed.configureTestingModule({ |
| 30 | + imports: [ |
| 31 | + HttpClientTestingModule, |
| 32 | + ], |
| 33 | + providers: [ |
| 34 | + FeedService, |
| 35 | + { |
| 36 | + provide: AuthService, |
| 37 | + useValue: spyAuth, |
| 38 | + }, |
| 39 | + ], |
| 40 | + }); |
| 41 | + |
| 42 | + feedService = TestBed.inject(FeedService); |
| 43 | + httpController = TestBed.inject(HttpTestingController); |
| 44 | + spyAuthService = TestBed.inject(AuthService) as jasmine.SpyObj<AuthService>; |
| 45 | + }); |
| 46 | + |
| 47 | + afterEach(() => { |
| 48 | + httpController.verify(); |
| 49 | + }); |
| 50 | + |
| 51 | + it('should be created', () => { |
| 52 | + expect(feedService).toBeTruthy(); |
| 53 | + }); |
| 54 | + |
| 55 | + describe('get feeds function', () => { |
| 56 | + it('should return feeds when user is not authenticated', (doneFn) => { |
| 57 | + const mockFeeds: Feed[] = getFeedsMock(); |
| 58 | + const options = { skip: 0, limit: 10 }; |
| 59 | + |
| 60 | + feedService.getFeeds(options.skip, options.limit).subscribe((feeds) => { |
| 61 | + expect(feeds).toEqual(mockFeeds); |
| 62 | + expect(feeds.length).toBe(mockFeeds.length); |
| 63 | + doneFn(); |
| 64 | + }); |
| 65 | + |
| 66 | + const url = `${base_url}/feed?skip=${options.skip}&limit=${options.limit}`; |
| 67 | + const req = httpController.expectOne(url); |
| 68 | + req.flush({ ok: true, feeds: mockFeeds }); |
| 69 | + |
| 70 | + expect(req.request.method).toBe('GET'); |
| 71 | + expect(req.request.headers.has('x-token')).toBe(false); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should return feeds when user is authenticated', (doneFn) => { |
| 75 | + const mockFeeds: Feed[] = getFeedsMock(); |
| 76 | + const options = { skip: 0, limit: 10 }; |
| 77 | + |
| 78 | + feedService.getFeeds(options.skip, options.limit, true).subscribe((feeds) => { |
| 79 | + expect(feeds).toEqual(mockFeeds); |
| 80 | + expect(feeds.length).toBe(mockFeeds.length); |
| 81 | + doneFn(); |
| 82 | + }); |
| 83 | + |
| 84 | + const url = base_url + '/feed/byUser/subscription?skip=' + options.skip |
| 85 | + + '&limit=' + options.limit; |
| 86 | + const req = httpController.expectOne(url); |
| 87 | + req.flush({ ok: true, feeds: mockFeeds }); |
| 88 | + |
| 89 | + expect(req.request.method).toBe('GET'); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should return by default 10 feeds from offset 0', (doneFn) => { |
| 93 | + const options = { skip: 0, limit: 10 }; |
| 94 | + const mockFeeds: Feed[] = getFeedsMock(options.limit); |
| 95 | + |
| 96 | + feedService.getFeeds().subscribe((feeds) => { |
| 97 | + expect(feeds).toEqual(mockFeeds); |
| 98 | + expect(feeds.length).toBe(mockFeeds.length); |
| 99 | + doneFn(); |
| 100 | + }); |
| 101 | + |
| 102 | + const url = `${base_url}/feed?skip=${options.skip}&limit=${options.limit}`; |
| 103 | + const req = httpController.expectOne(url); |
| 104 | + req.flush({ ok: true, feeds: mockFeeds }); |
| 105 | + |
| 106 | + expect(req.request.method).toBe('GET'); |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + describe('get feed by id function', () => { |
| 111 | + it('should return feed by id = 1234', (doneFn) => { |
| 112 | + const mockFeed: Feed = getFeedMock(); |
| 113 | + mockFeed._id = '1234'; |
| 114 | + |
| 115 | + feedService.getFeed(mockFeed._id).subscribe((feed) => { |
| 116 | + expect(feed).toEqual(mockFeed); |
| 117 | + expect(feed._id).toBe(mockFeed._id); |
| 118 | + doneFn(); |
| 119 | + }); |
| 120 | + |
| 121 | + const url = `${base_url}/feed/${mockFeed._id}`; |
| 122 | + const req = httpController.expectOne(url); |
| 123 | + req.flush({ ok: true, feed: mockFeed }); |
| 124 | + |
| 125 | + expect(req.request.method).toBe('GET'); |
| 126 | + }); |
| 127 | + }); |
| 128 | + |
| 129 | + describe('saved feeds function', () => { |
| 130 | + it('should return saved feeds', (doneFn) => { |
| 131 | + const mockFeeds: Feed[] = getFeedsMock(); |
| 132 | + const options = { skip: 0, limit: 10 }; |
| 133 | + |
| 134 | + feedService.getSavedFeeds().subscribe((feeds) => { |
| 135 | + expect(feeds).toEqual(mockFeeds); |
| 136 | + expect(feeds.length).toBe(mockFeeds.length); |
| 137 | + doneFn(); |
| 138 | + }); |
| 139 | + |
| 140 | + const url = base_url + '/feed/byUser/saved' + '?skip=' |
| 141 | + + options.skip + '&limit=' + options.limit; |
| 142 | + const req = httpController.expectOne(url); |
| 143 | + req.flush({ ok: true, feeds: mockFeeds }); |
| 144 | + |
| 145 | + expect(req.request.method).toBe('GET'); |
| 146 | + }); |
| 147 | + |
| 148 | + it('should return saved feeds by default 10 feeds from offset 0', (doneFn) => { |
| 149 | + const options = { skip: 0, limit: 10 }; |
| 150 | + const mockFeeds: Feed[] = getFeedsMock(options.limit); |
| 151 | + |
| 152 | + feedService.getSavedFeeds().subscribe((feeds) => { |
| 153 | + expect(feeds).toEqual(mockFeeds); |
| 154 | + expect(feeds.length).toBe(mockFeeds.length); |
| 155 | + doneFn(); |
| 156 | + }); |
| 157 | + |
| 158 | + const url = base_url + '/feed/byUser/saved' + '?skip=' |
| 159 | + + options.skip + '&limit=' + options.limit; |
| 160 | + const req = httpController.expectOne(url); |
| 161 | + req.flush({ ok: true, feeds: mockFeeds }); |
| 162 | + |
| 163 | + expect(req.request.method).toBe('GET'); |
| 164 | + }); |
| 165 | + }); |
| 166 | + |
| 167 | + describe('search feeds function', () => { |
| 168 | + it('should return feeds by search = "test"', (doneFn) => { |
| 169 | + const options = { skip: 0, limit: 10 }; |
| 170 | + const mockFeeds: Feed[] = getFeedsMock(options.limit); |
| 171 | + const search = 'test'; |
| 172 | + |
| 173 | + feedService.searchFeeds(options.skip, options.limit, search).subscribe((feeds) => { |
| 174 | + expect(feeds).toEqual(mockFeeds); |
| 175 | + expect(feeds.length).toBe(mockFeeds.length); |
| 176 | + doneFn(); |
| 177 | + }); |
| 178 | + |
| 179 | + const url = base_url + '/feed/search' + '?skip=' + options.skip + '&limit=' |
| 180 | + + options.limit + '&q=' + search; |
| 181 | + const req = httpController.expectOne(url); |
| 182 | + req.flush({ ok: true, feeds: mockFeeds }); |
| 183 | + |
| 184 | + expect(req.request.method).toBe('GET'); |
| 185 | + }); |
| 186 | + |
| 187 | + it('should return feeds by search = "test" by default ' |
| 188 | + +'10 feeds from offset 0', (doneFn) => { |
| 189 | + const options = { skip: 0, limit: 10 }; |
| 190 | + const mockFeeds: Feed[] = getFeedsMock(options.limit); |
| 191 | + const search = 'test'; |
| 192 | + |
| 193 | + feedService.searchFeeds(undefined, undefined, search).subscribe((feeds) => { |
| 194 | + expect(feeds).toEqual(mockFeeds); |
| 195 | + expect(feeds.length).toBe(mockFeeds.length); |
| 196 | + doneFn(); |
| 197 | + }); |
| 198 | + |
| 199 | + const url = base_url + '/feed/search' + '?skip=' + options.skip + '&limit=' |
| 200 | + + options.limit + '&q=' + search; |
| 201 | + const req = httpController.expectOne(url); |
| 202 | + req.flush({ ok: true, feeds: mockFeeds }); |
| 203 | + |
| 204 | + expect(req.request.method).toBe('GET'); |
| 205 | + }); |
| 206 | + }); |
| 207 | + |
| 208 | + describe('map in user resource function', () => { |
| 209 | + it('should return feeds without changes if user is not authenticated', () => { |
| 210 | + const mockFeeds: Feed[] = getFeedsMock(); |
| 211 | + const feeds = feedService.mapInUserResource(mockFeeds); |
| 212 | + |
| 213 | + expect(feeds).toEqual(mockFeeds); |
| 214 | + }); |
| 215 | + |
| 216 | + it('should return feeds with user resource if user is authenticated', () => { |
| 217 | + const mockFeeds: Feed[] = getFeedsMock(2); |
| 218 | + const mockUser: User = getUserMock(); |
| 219 | + |
| 220 | + spyAuthService.getUserActive.and.returnValue(mockUser); |
| 221 | + |
| 222 | + const feeds = feedService.mapInUserResource(mockFeeds); |
| 223 | + |
| 224 | + expect(feeds).toEqual(mockFeeds); |
| 225 | + }); |
| 226 | + |
| 227 | + it('should return feed with user resource if user is authenticated', () => { |
| 228 | + const mockFeed: Feed = getFeedMock(); |
| 229 | + const mockUser: User = getUserMock(); |
| 230 | + |
| 231 | + spyAuthService.getUserActive.and.returnValue(mockUser); |
| 232 | + |
| 233 | + const feeds = feedService.mapInUserResource(mockFeed); |
| 234 | + |
| 235 | + expect(feeds).toEqual(mockFeed); |
| 236 | + }); |
| 237 | + }); |
| 238 | +}); |
0 commit comments