Skip to content

Commit dd27152

Browse files
committed
test: improve coverage
1 parent 9279276 commit dd27152

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

src/profile/data/sagas.test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ import {
77
all,
88
} from 'redux-saga/effects';
99
import { getAuthenticatedUser } from '@edx/frontend-platform/auth';
10+
import { runSaga } from 'redux-saga';
1011

1112
import * as profileActions from './actions';
13+
import { getExtendedProfileFields } from './services';
1214
import { handleSaveProfileSelector, userAccountSelector } from './selectors';
1315

1416
jest.mock('./services', () => ({
@@ -19,6 +21,7 @@ jest.mock('./services', () => ({
1921
getPreferences: jest.fn(),
2022
getAccount: jest.fn(),
2123
getCourseCertificates: jest.fn(),
24+
getExtendedProfileFields: jest.fn(),
2225
}));
2326

2427
jest.mock('@edx/frontend-platform/auth', () => ({
@@ -166,4 +169,43 @@ describe('RootSaga', () => {
166169
expect(gen.next().value).toBeUndefined();
167170
});
168171
});
172+
173+
describe('fetchThirdPartyAuthContext', () => {
174+
test('should fetch third party auth context', async () => {
175+
const dispatched = [];
176+
const mockedAction = { payload: { urlParams: {} } };
177+
const mockFields = { field1: 'value1' };
178+
179+
getExtendedProfileFields.mockResolvedValue({ fields: mockFields });
180+
181+
await runSaga(
182+
{
183+
dispatch: (action) => dispatched.push(action),
184+
},
185+
fetchThirdPartyAuthContext,
186+
mockedAction,
187+
).toPromise();
188+
189+
expect(dispatched).toContainEqual(profileActions.getExtendedProfileFieldsBegin());
190+
expect(dispatched).toContainEqual(profileActions.getExtendedProfileFieldsSuccess(mockFields));
191+
});
192+
193+
test('should fail to fetch third party auth context', async () => {
194+
const dispatched = [];
195+
const mockedAction = { payload: { urlParams: {} } };
196+
197+
getExtendedProfileFields.mockRejectedValue(new Error('API error'));
198+
199+
await expect(runSaga(
200+
{
201+
dispatch: (action) => dispatched.push(action),
202+
},
203+
fetchThirdPartyAuthContext,
204+
mockedAction,
205+
).toPromise()).rejects.toThrow('API error');
206+
207+
expect(dispatched).toContainEqual(profileActions.getExtendedProfileFieldsBegin());
208+
expect(dispatched).toContainEqual(profileActions.getExtendedProfileFieldsFailure());
209+
});
210+
});
169211
});

src/profile/utils.test.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import {
44
camelCaseObject,
55
snakeCaseObject,
66
convertKeyNames,
7+
moveCheckboxFieldsToEnd,
8+
capitalizeFirstLetter,
79
} from './utils';
810

911
describe('modifyObjectKeys', () => {
@@ -100,4 +102,68 @@ describe('AsyncActionType', () => {
100102
expect(actionType.FAILURE).toBe('HOUSE_CATS__START_THE_RACE__FAILURE');
101103
expect(actionType.RESET).toBe('HOUSE_CATS__START_THE_RACE__RESET');
102104
});
105+
106+
describe('moveCheckboxFieldsToEnd', () => {
107+
it('returns 1 when first field is checkbox and second field is not', () => {
108+
const a = { type: 'checkbox' };
109+
const b = { type: 'text' };
110+
111+
expect(moveCheckboxFieldsToEnd(a, b)).toEqual(1);
112+
});
113+
114+
it('returns -1 when first field is not checkbox and second field is', () => {
115+
const a = { type: 'text' };
116+
const b = { type: 'checkbox' };
117+
118+
expect(moveCheckboxFieldsToEnd(a, b)).toEqual(-1);
119+
});
120+
121+
it('returns 0 when both fields are checkboxes', () => {
122+
const a = { type: 'checkbox' };
123+
const b = { type: 'checkbox' };
124+
125+
expect(moveCheckboxFieldsToEnd(a, b)).toEqual(0);
126+
});
127+
128+
it('returns 0 when neither field is a checkbox', () => {
129+
const a = { type: 'text' };
130+
const b = { type: 'text' };
131+
132+
expect(moveCheckboxFieldsToEnd(a, b)).toEqual(0);
133+
});
134+
});
135+
136+
describe('capitalizeFirstLetter', () => {
137+
it('capitalizes the first letter of a string', () => {
138+
expect(capitalizeFirstLetter('hello')).toBe('Hello');
139+
});
140+
141+
it('returns an empty string when given an empty string', () => {
142+
expect(capitalizeFirstLetter('')).toBe('');
143+
});
144+
145+
it('handles single character strings', () => {
146+
expect(capitalizeFirstLetter('a')).toBe('A');
147+
});
148+
149+
it('handles strings with only one word', () => {
150+
expect(capitalizeFirstLetter('word')).toBe('Word');
151+
});
152+
153+
it('handles strings with multiple words', () => {
154+
expect(capitalizeFirstLetter('multiple words')).toBe('Multiple words');
155+
});
156+
157+
it('handles non-string inputs by converting them to strings', () => {
158+
expect(capitalizeFirstLetter(123)).toBe('123');
159+
expect(capitalizeFirstLetter(null)).toBe('Null');
160+
expect(capitalizeFirstLetter(undefined)).toBe('Undefined');
161+
expect(capitalizeFirstLetter(true)).toBe('True');
162+
});
163+
164+
it('handles strings with special characters', () => {
165+
expect(capitalizeFirstLetter('!hello')).toBe('!hello');
166+
expect(capitalizeFirstLetter('@world')).toBe('@world');
167+
});
168+
});
103169
});

0 commit comments

Comments
 (0)