|
1 | | -// import { TestBed } from '@angular/core/testing'; |
2 | | -// |
3 | | -// import { ArticleService } from './article.service'; |
4 | | -// |
5 | | -// describe('ArticleService', () => { |
6 | | -// let service: ArticleService; |
7 | | -// |
8 | | -// beforeEach(() => { |
9 | | -// TestBed.configureTestingModule({}); |
10 | | -// service = TestBed.inject(ArticleService); |
11 | | -// }); |
12 | | -// |
13 | | -// it('should be created', () => { |
14 | | -// // expect(service).toBeTruthy(); |
15 | | -// }); |
16 | | -// }); |
| 1 | +import { TestBed } from '@angular/core/testing'; |
| 2 | + |
| 3 | +import { ArticleService } from './article.service'; |
| 4 | +import { RequestHelperService } from "../utils/request-helper.service"; |
| 5 | +import { of } from "rxjs"; |
| 6 | +import { CreateArticlePayload } from "../../models/api/article.model"; |
| 7 | + |
| 8 | +describe('ArticleService', () => { |
| 9 | + let service: ArticleService; |
| 10 | + let spyRequestHelperService: Partial<jasmine.SpyObj<RequestHelperService>>; |
| 11 | + |
| 12 | + beforeEach(()=> { |
| 13 | + spyRequestHelperService = { |
| 14 | + get: jasmine.createSpy('get'), |
| 15 | + post: jasmine.createSpy('post'), |
| 16 | + put: jasmine.createSpy('put'), |
| 17 | + delete: jasmine.createSpy('delete') |
| 18 | + } |
| 19 | + spyRequestHelperService.get!.and.returnValue(of(null)); |
| 20 | + spyRequestHelperService.post!.and.returnValue(of(null)); |
| 21 | + spyRequestHelperService.put!.and.returnValue(of(null)); |
| 22 | + spyRequestHelperService.delete!.and.returnValue(of(null)); |
| 23 | + }); |
| 24 | + |
| 25 | + beforeEach(() => { |
| 26 | + TestBed.configureTestingModule({ |
| 27 | + providers: [ |
| 28 | + ArticleService, |
| 29 | + { provide: RequestHelperService, useValue: spyRequestHelperService } |
| 30 | + ] |
| 31 | + }); |
| 32 | + service = TestBed.inject(ArticleService); |
| 33 | + }); |
| 34 | + |
| 35 | + it('should be created', () => { |
| 36 | + expect(service).toBeTruthy(); |
| 37 | + }); |
| 38 | + |
| 39 | + it('should create article', ()=> { |
| 40 | + const expectedPayload: CreateArticlePayload = { |
| 41 | + article: { |
| 42 | + title: 'title', |
| 43 | + description: 'description', |
| 44 | + body: 'body', |
| 45 | + tagList: ['tag1', 'tag2'] |
| 46 | + } |
| 47 | + } |
| 48 | + service.createArticle(expectedPayload); |
| 49 | + expect(spyRequestHelperService.post).toHaveBeenCalledWith('/articles', expectedPayload); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should get article', ()=> { |
| 53 | + const expectedSlug = 'slug'; |
| 54 | + service.getArticle(expectedSlug); |
| 55 | + expect(spyRequestHelperService.get).toHaveBeenCalledWith(`/articles/${expectedSlug}`); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should update article', ()=> { |
| 59 | + const expectedSlug = 'slug'; |
| 60 | + const expectedPayload = { article: { title: 'title' } }; |
| 61 | + service.updateArticle(expectedSlug, expectedPayload); |
| 62 | + expect(spyRequestHelperService.put).toHaveBeenCalledWith(`/articles/${expectedSlug}`, expectedPayload); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should delete article', ()=> { |
| 66 | + const expectedSlug = 'slug'; |
| 67 | + service.deleteArticle(expectedSlug); |
| 68 | + expect(spyRequestHelperService.delete).toHaveBeenCalledWith(`/articles/${expectedSlug}`); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should query articles', ()=> { |
| 72 | + const expectedParams = { tag: 'tag' }; |
| 73 | + service.queryArticles(expectedParams); |
| 74 | + expect(spyRequestHelperService.get).toHaveBeenCalledWith('/articles', {params: expectedParams}); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should query feed articles', ()=> { |
| 78 | + const expectedParams = { tag: 'tag' }; |
| 79 | + service.queryFeedArticles(expectedParams); |
| 80 | + expect(spyRequestHelperService.get).toHaveBeenCalledWith('/articles/feed', {params: expectedParams}); |
| 81 | + }); |
| 82 | + |
| 83 | + it('should favorite article', ()=> { |
| 84 | + const expectedSlug = 'slug'; |
| 85 | + service.favoriteArticle(expectedSlug); |
| 86 | + expect(spyRequestHelperService.post).toHaveBeenCalledWith(`/articles/${expectedSlug}/favorite`, null); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should unfavorite article', ()=> { |
| 90 | + const expectedSlug = 'slug'; |
| 91 | + service.unfavoriteArticle(expectedSlug); |
| 92 | + expect(spyRequestHelperService.delete).toHaveBeenCalledWith(`/articles/${expectedSlug}/favorite`); |
| 93 | + }); |
| 94 | + |
| 95 | + it('should query article comments', ()=> { |
| 96 | + const expectedSlug = 'slug'; |
| 97 | + service.queryArticleComments(expectedSlug); |
| 98 | + expect(spyRequestHelperService.get).toHaveBeenCalledWith(`/articles/${expectedSlug}/comments`); |
| 99 | + }); |
| 100 | + |
| 101 | + it('should create article comment', ()=> { |
| 102 | + const expectedSlug = 'slug'; |
| 103 | + const expectedPayload = { comment: { body: 'body' } }; |
| 104 | + service.createArticleComment(expectedSlug, expectedPayload); |
| 105 | + expect(spyRequestHelperService.post).toHaveBeenCalledWith(`/articles/${expectedSlug}/comments`, expectedPayload); |
| 106 | + }); |
| 107 | + |
| 108 | + it('should delete article comment', ()=> { |
| 109 | + const expectedSlug = 'slug'; |
| 110 | + const expectedCommentId = 2; |
| 111 | + service.deleteArticleComment(expectedSlug, expectedCommentId); |
| 112 | + expect(spyRequestHelperService.delete).toHaveBeenCalledWith(`/articles/${expectedSlug}/comments/${expectedCommentId}`); |
| 113 | + }); |
| 114 | +}); |
0 commit comments