|
| 1 | +import { getAuthenticatedHttpClient } from '@edx/frontend-platform/auth'; |
| 2 | +import { logError } from '@edx/frontend-platform/logging'; |
| 3 | +import { |
| 4 | + getAccount, |
| 5 | + patchProfile, |
| 6 | + postProfilePhoto, |
| 7 | + deleteProfilePhoto, |
| 8 | + getPreferences, |
| 9 | + patchPreferences, |
| 10 | + getCourseCertificates, |
| 11 | + getCountryList, |
| 12 | +} from './services'; |
| 13 | + |
| 14 | +import { FIELD_LABELS } from './constants'; |
| 15 | + |
| 16 | +import { camelCaseObject, snakeCaseObject, convertKeyNames } from '../utils'; |
| 17 | + |
| 18 | +// --- Mocks --- |
| 19 | +jest.mock('@edx/frontend-platform', () => ({ |
| 20 | + ensureConfig: jest.fn(), |
| 21 | + getConfig: jest.fn(() => ({ LMS_BASE_URL: 'http://fake-lms' })), |
| 22 | +})); |
| 23 | + |
| 24 | +jest.mock('@edx/frontend-platform/auth', () => ({ |
| 25 | + getAuthenticatedHttpClient: jest.fn(), |
| 26 | +})); |
| 27 | + |
| 28 | +jest.mock('@edx/frontend-platform/logging', () => ({ |
| 29 | + logError: jest.fn(), |
| 30 | +})); |
| 31 | + |
| 32 | +jest.mock('../utils', () => ({ |
| 33 | + camelCaseObject: jest.fn((obj) => obj), |
| 34 | + snakeCaseObject: jest.fn((obj) => obj), |
| 35 | + convertKeyNames: jest.fn((obj) => obj), |
| 36 | +})); |
| 37 | + |
| 38 | +const mockHttpClient = { |
| 39 | + get: jest.fn(), |
| 40 | + patch: jest.fn(), |
| 41 | + post: jest.fn(), |
| 42 | + delete: jest.fn(), |
| 43 | +}; |
| 44 | + |
| 45 | +beforeEach(() => { |
| 46 | + jest.clearAllMocks(); |
| 47 | + getAuthenticatedHttpClient.mockReturnValue(mockHttpClient); |
| 48 | +}); |
| 49 | + |
| 50 | +// --- Tests --- |
| 51 | +describe('services', () => { |
| 52 | + describe('getAccount', () => { |
| 53 | + it('should return processed account data', async () => { |
| 54 | + const mockData = { name: 'John Doe', socialLinks: [] }; |
| 55 | + mockHttpClient.get.mockResolvedValue({ data: mockData }); |
| 56 | + |
| 57 | + const result = await getAccount('john'); |
| 58 | + expect(result).toMatchObject(mockData); |
| 59 | + expect(mockHttpClient.get).toHaveBeenCalledWith( |
| 60 | + 'http://fake-lms/api/user/v1/accounts/john', |
| 61 | + ); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + describe('patchProfile', () => { |
| 66 | + it('should patch and return processed data', async () => { |
| 67 | + const mockData = { bio: 'New Bio' }; |
| 68 | + mockHttpClient.patch.mockResolvedValue({ data: mockData }); |
| 69 | + |
| 70 | + const result = await patchProfile('john', { bio: 'New Bio' }); |
| 71 | + expect(result).toMatchObject(mockData); |
| 72 | + expect(snakeCaseObject).toHaveBeenCalledWith({ bio: 'New Bio' }); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should throw processed error on failure', async () => { |
| 76 | + const error = { response: { data: { some: 'error' } } }; |
| 77 | + mockHttpClient.patch.mockRejectedValue(error); |
| 78 | + |
| 79 | + await expect(patchProfile('john', {})).rejects.toMatchObject(error); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + describe('postProfilePhoto', () => { |
| 84 | + it('should post photo and return updated profile image', async () => { |
| 85 | + mockHttpClient.post.mockResolvedValue({}); |
| 86 | + mockHttpClient.get.mockResolvedValue({ |
| 87 | + data: { profileImage: { url: 'img.png' } }, |
| 88 | + }); |
| 89 | + |
| 90 | + const result = await postProfilePhoto('john', new FormData()); |
| 91 | + expect(result).toEqual({ url: 'img.png' }); |
| 92 | + }); |
| 93 | + |
| 94 | + it('should throw error if API fails', async () => { |
| 95 | + const error = { response: { data: { error: 'fail' } } }; |
| 96 | + mockHttpClient.post.mockRejectedValue(error); |
| 97 | + await expect(postProfilePhoto('john', new FormData())).rejects.toMatchObject(error); |
| 98 | + }); |
| 99 | + }); |
| 100 | + |
| 101 | + describe('deleteProfilePhoto', () => { |
| 102 | + it('should delete photo and return updated profile image', async () => { |
| 103 | + mockHttpClient.delete.mockResolvedValue({}); |
| 104 | + mockHttpClient.get.mockResolvedValue({ |
| 105 | + data: { profileImage: { url: 'deleted.png' } }, |
| 106 | + }); |
| 107 | + |
| 108 | + const result = await deleteProfilePhoto('john'); |
| 109 | + expect(result).toEqual({ url: 'deleted.png' }); |
| 110 | + }); |
| 111 | + }); |
| 112 | + |
| 113 | + describe('getPreferences', () => { |
| 114 | + it('should return camelCased preferences', async () => { |
| 115 | + mockHttpClient.get.mockResolvedValue({ data: { pref: 1 } }); |
| 116 | + |
| 117 | + const result = await getPreferences('john'); |
| 118 | + expect(result).toMatchObject({ pref: 1 }); |
| 119 | + expect(camelCaseObject).toHaveBeenCalledWith({ pref: 1 }); |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + describe('patchPreferences', () => { |
| 124 | + it('should patch preferences and return params', async () => { |
| 125 | + mockHttpClient.patch.mockResolvedValue({}); |
| 126 | + const params = { visibility_bio: true }; |
| 127 | + |
| 128 | + const result = await patchPreferences('john', params); |
| 129 | + expect(result).toBe(params); |
| 130 | + expect(snakeCaseObject).toHaveBeenCalledWith(params); |
| 131 | + expect(convertKeyNames).toHaveBeenCalled(); |
| 132 | + }); |
| 133 | + }); |
| 134 | + |
| 135 | + describe('getCourseCertificates', () => { |
| 136 | + it('should return transformed certificates', async () => { |
| 137 | + mockHttpClient.get.mockResolvedValue({ |
| 138 | + data: [{ download_url: '/path', certificate_type: 'type' }], |
| 139 | + }); |
| 140 | + |
| 141 | + const result = await getCourseCertificates('john'); |
| 142 | + expect(result[0]).toHaveProperty('downloadUrl', 'http://fake-lms/path'); |
| 143 | + }); |
| 144 | + |
| 145 | + it('should log error and return empty array on failure', async () => { |
| 146 | + mockHttpClient.get.mockRejectedValue(new Error('fail')); |
| 147 | + const result = await getCourseCertificates('john'); |
| 148 | + expect(result).toEqual([]); |
| 149 | + expect(logError).toHaveBeenCalled(); |
| 150 | + }); |
| 151 | + }); |
| 152 | + |
| 153 | + describe('getCountryList', () => { |
| 154 | + it('should extract country list', async () => { |
| 155 | + mockHttpClient.get.mockResolvedValue({ |
| 156 | + data: { |
| 157 | + fields: [ |
| 158 | + { name: FIELD_LABELS.COUNTRY, options: [{ value: 'US' }, { value: 'CA' }] }, |
| 159 | + ], |
| 160 | + }, |
| 161 | + }); |
| 162 | + |
| 163 | + const result = await getCountryList(); |
| 164 | + expect(result).toEqual(['US', 'CA']); |
| 165 | + }); |
| 166 | + |
| 167 | + it('should log error and return empty array on failure', async () => { |
| 168 | + mockHttpClient.get.mockRejectedValue(new Error('fail')); |
| 169 | + const result = await getCountryList(); |
| 170 | + expect(result).toEqual([]); |
| 171 | + expect(logError).toHaveBeenCalled(); |
| 172 | + }); |
| 173 | + }); |
| 174 | +}); |
0 commit comments