|
| 1 | +const { XkcdComic } = require('../../dto/XkcdComic'); |
| 2 | +const { XkcdComicService } = require('../XkcdComicService'); |
| 3 | +const LocalPath = require('../../const/LocalPath'); |
| 4 | + |
| 5 | +jest.mock('../../dto/XkcdComic'); |
| 6 | + |
| 7 | +describe('XkcdComicService', () => { |
| 8 | + it('should get random comic', async () => { |
| 9 | + const Request = (global.Request = jest.fn(() => Request)); |
| 10 | + |
| 11 | + const fileUtil = jest.fn(); |
| 12 | + const imageUtil = jest.fn(); |
| 13 | + const numberUtil = jest.fn(); |
| 14 | + const service = new XkcdComicService(fileUtil, imageUtil, numberUtil); |
| 15 | + |
| 16 | + const randomComicNumber = 1234; |
| 17 | + const comicRESTURL = 'https://xkcd.com/1234/info.0.json'; |
| 18 | + const imageURL = 'https://imgs.xkcd.com/comics/ios_keyboard_2x.png'; |
| 19 | + const title = 'abcdef'; |
| 20 | + const image = new Object(); |
| 21 | + |
| 22 | + jest.spyOn(service, 'getRandomComicNumber').mockReturnValueOnce(randomComicNumber); |
| 23 | + jest.spyOn(XkcdComic, 'getComicRESTURL').mockReturnValueOnce(comicRESTURL); |
| 24 | + |
| 25 | + Request.loadJSON = jest.fn(); |
| 26 | + Request.loadJSON.mockImplementation(() => { |
| 27 | + return new Promise((resolve, reject) => { |
| 28 | + resolve({ img: imageURL, title }); |
| 29 | + }); |
| 30 | + }); |
| 31 | + |
| 32 | + Request.loadImage = jest.fn(); |
| 33 | + Request.loadImage.mockImplementation(() => { |
| 34 | + return new Promise((resolve, reject) => { |
| 35 | + resolve(image); |
| 36 | + }); |
| 37 | + }); |
| 38 | + |
| 39 | + const result = await service.getRandomComic(); |
| 40 | + |
| 41 | + expect(result).toBeInstanceOf(XkcdComic); |
| 42 | + expect(Request.loadJSON).toHaveBeenCalled(); |
| 43 | + expect(Request.loadImage).toHaveBeenCalled(); |
| 44 | + expect(XkcdComic).toHaveBeenCalledWith(image, imageURL, title, randomComicNumber); |
| 45 | + |
| 46 | + expect(fileUtil).not.toHaveBeenCalled(); |
| 47 | + expect(imageUtil).not.toHaveBeenCalled(); |
| 48 | + expect(numberUtil).not.toHaveBeenCalled(); |
| 49 | + }); |
| 50 | + |
| 51 | + it.each([ |
| 52 | + [true, true], |
| 53 | + [true, false], |
| 54 | + [false, true], |
| 55 | + [false, false], |
| 56 | + ])('should cache comic', (isDirectory, fileExists) => { |
| 57 | + const fileUtil = jest.fn(); |
| 58 | + const imageUtil = jest.fn(); |
| 59 | + const numberUtil = jest.fn(); |
| 60 | + const service = new XkcdComicService(fileUtil, imageUtil, numberUtil); |
| 61 | + |
| 62 | + const documentsDir = 'abcd/efgh'; |
| 63 | + const cacheDir = documentsDir + '/' + LocalPath.LOCAL_CACHE_DIRNAME; |
| 64 | + const cacheFilePath = cacheDir + '/' + LocalPath.XKCD_CACHE_FILENAME; |
| 65 | + |
| 66 | + const FileManager = (global.FileManager = jest.fn(() => FileManager)); |
| 67 | + |
| 68 | + const iCloudMock = { |
| 69 | + documentsDirectory: jest.fn().mockReturnValueOnce(documentsDir), |
| 70 | + joinPath: jest.fn((left, right) => left + '/' + right), |
| 71 | + isDirectory: jest.fn().mockReturnValueOnce(isDirectory), |
| 72 | + createDirectory: jest.fn(), |
| 73 | + fileExists: jest.fn().mockReturnValueOnce(fileExists), |
| 74 | + remove: jest.fn(), |
| 75 | + writeString: jest.fn(), |
| 76 | + }; |
| 77 | + FileManager.iCloud = jest.fn(() => iCloudMock); |
| 78 | + |
| 79 | + fileUtil.getFileExtension = jest.fn(); |
| 80 | + fileUtil.getFileExtension.mockReturnValue('jpg'); |
| 81 | + const base64EncodedImage = 'very-long-base64-string'; |
| 82 | + imageUtil.base64EncodeImage = jest.fn(); |
| 83 | + imageUtil.base64EncodeImage.mockReturnValue(base64EncodedImage); |
| 84 | + |
| 85 | + const title = 'foo'; |
| 86 | + const xkcdURL = 'https://aksjdnkasd.com'; |
| 87 | + const imageURL = 'https://abc.com/foo.jpg'; |
| 88 | + const comic = { |
| 89 | + title, |
| 90 | + xkcdURL, |
| 91 | + imageURL, |
| 92 | + }; |
| 93 | + service.cacheComic(comic); |
| 94 | + |
| 95 | + if (isDirectory) { |
| 96 | + expect(iCloudMock.createDirectory).not.toHaveBeenCalled(); |
| 97 | + } else { |
| 98 | + expect(iCloudMock.createDirectory).toHaveBeenCalledWith(cacheDir, true); |
| 99 | + } |
| 100 | + if (fileExists) { |
| 101 | + expect(iCloudMock.remove).toHaveBeenCalledWith(cacheFilePath); |
| 102 | + } else { |
| 103 | + expect(iCloudMock.remove).not.toHaveBeenCalled(); |
| 104 | + } |
| 105 | + |
| 106 | + expect(iCloudMock.writeString).toHaveBeenCalledWith( |
| 107 | + cacheFilePath, |
| 108 | + JSON.stringify( |
| 109 | + { |
| 110 | + title, |
| 111 | + xkcdURL, |
| 112 | + image: { |
| 113 | + url: imageURL, |
| 114 | + base64: base64EncodedImage, |
| 115 | + }, |
| 116 | + }, |
| 117 | + null, |
| 118 | + 4 |
| 119 | + ) |
| 120 | + ); |
| 121 | + expect(numberUtil).not.toHaveBeenCalled(); |
| 122 | + }); |
| 123 | +}); |
0 commit comments