Skip to content

Commit

Permalink
Add getCurrentSchoolyear()
Browse files Browse the repository at this point in the history
  • Loading branch information
nkilders committed Feb 12, 2024
1 parent 2c51a44 commit e801775
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 25 deletions.
50 changes: 25 additions & 25 deletions __tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ const initMocks = () => {
baseURL,
expect.objectContaining({
method: 'getLatestImportTime',
})
}),
)
.replyOnce(200, { result: 123 });
};
Expand Down Expand Up @@ -68,7 +68,7 @@ cases(
{ name: 'with result null', response: { result: null } },
{ name: 'with result has code', response: { result: { code: 500 } } },
{ name: 'with empty sessionId', response: { result: {} } },
]
],
);

test('should method logout return true', async () => {
Expand Down Expand Up @@ -113,7 +113,7 @@ cases(
[
{ name: 'with validate', validate: true },
{ name: 'without validate', validate: false },
]
],
);

test('should getLatestSchoolyear throw error with empty array', async () => {
Expand Down Expand Up @@ -141,7 +141,7 @@ cases(
[
{ name: 'with validate', validate: true },
{ name: 'without validate', validate: false },
]
],
);

test('should getNewsWidget catch invalid data', async () => {
Expand All @@ -154,7 +154,7 @@ test('should getNewsWidget catch invalid data', async () => {
baseURL,
expect.objectContaining({
method: 'getLatestImportTime',
})
}),
)
.replyOnce(200, { result: 'string' });

Expand Down Expand Up @@ -184,7 +184,7 @@ cases(
[
{ name: 'with validate', validate: true },
{ name: 'without validate', validate: false },
]
],
);

cases(
Expand All @@ -207,7 +207,7 @@ cases(
[
{ name: 'with validate', postIndex: 2, validate: true },
{ name: 'without validate', postIndex: 1, validate: false },
]
],
);

cases(
Expand All @@ -225,7 +225,7 @@ cases(
[
{ name: 'with validate', postIndex: 1, validate: true },
{ name: 'without validate', postIndex: 0, validate: false },
]
],
);

cases(
Expand All @@ -249,7 +249,7 @@ cases(
[
{ name: 'with validate', postIndex: 2, validate: true },
{ name: 'without validate', postIndex: 1, validate: false },
]
],
);

cases(
Expand All @@ -269,7 +269,7 @@ cases(
[
{ name: 'with validate', validate: true },
{ name: 'without validate', validate: false },
]
],
);

cases(
Expand All @@ -288,7 +288,7 @@ cases(
[
{ name: 'with validate', validate: true },
{ name: 'without validate', validate: false },
]
],
);

cases(
Expand All @@ -310,7 +310,7 @@ cases(
[
{ name: 'with validate', validate: true },
{ name: 'without validate', validate: false },
]
],
);

cases(
Expand All @@ -328,13 +328,13 @@ cases(

expect(await untis.getOwnClassTimetableForToday(validate)).toBe(123);
expect(JSON.parse(mockAxios.history.post[2].data)).toMatchObject(
getElementObject(mockResponse.result.klasseId, 1)
getElementObject(mockResponse.result.klasseId, 1),
);
},
[
{ name: 'with validate', validate: true },
{ name: 'without validate', validate: false },
]
],
);

cases(
Expand All @@ -354,13 +354,13 @@ cases(
expect(await untis.getOwnClassTimetableFor(date, validate)).toBe(123);
expect(mockAxios.history.post[2].data).toMatch('20191113');
expect(JSON.parse(mockAxios.history.post[2].data)).toMatchObject(
getElementObject(mockResponse.result.klasseId, 1)
getElementObject(mockResponse.result.klasseId, 1),
);
},
[
{ name: 'with validate', validate: true },
{ name: 'without validate', validate: false },
]
],
);

cases(
Expand All @@ -382,13 +382,13 @@ cases(
expect(mockAxios.history.post[2].data).toMatch('20191113');
expect(mockAxios.history.post[2].data).toMatch('20191117');
expect(JSON.parse(mockAxios.history.post[2].data)).toMatchObject(
getElementObject(mockResponse.result.klasseId, 1)
getElementObject(mockResponse.result.klasseId, 1),
);
},
[
{ name: 'with validate', validate: true },
{ name: 'without validate', validate: false },
]
],
);

cases(
Expand All @@ -413,7 +413,7 @@ cases(
[
{ name: 'with validate', validate: true },
{ name: 'without validate', validate: false },
]
],
);

cases(
Expand Down Expand Up @@ -469,7 +469,7 @@ cases(
data: {},
response: { result: 200 },
},
]
],
);

test('should convertUntisDate converted date', () => {
Expand Down Expand Up @@ -563,7 +563,7 @@ cases(
validate: false,
post: 0,
},
]
],
);

cases(
Expand All @@ -588,7 +588,7 @@ cases(
[
{ name: 'with validate', validate: true },
{ name: 'without validate', validate: false },
]
],
);

cases(
Expand All @@ -606,7 +606,7 @@ cases(
.replyOnce(200, { data });

await expect(() =>
untis.getHomeWorkAndLessons(dateStart, dateEnd, validate)
untis.getHomeWorkAndLessons(dateStart, dateEnd, validate),
).rejects.toThrowErrorMatchingSnapshot();
},
[
Expand Down Expand Up @@ -646,7 +646,7 @@ cases(
data: {},
response: { result: 200 },
},
]
],
);

cases(
Expand All @@ -663,5 +663,5 @@ cases(
date: '09/08/2019',
result: '20190908',
},
]
],
);
18 changes: 18 additions & 0 deletions src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,24 @@ export class Base {
return await this._request('getStatusData', {}, validateSession);
}

/**
* Get the current school year
* @param [validateSession=true]
* @returns {Promise.<SchoolYear>}
*/
async getCurrentSchoolyear(validateSession = true): Promise<SchoolYear> {
const data = await this._request<InternalSchoolYear>('getCurrentSchoolyear', {}, validateSession);

if (!data) throw new Error('Failed to retrieve current school year');

return {
name: data.name,
id: data.id,
startDate: Base.convertUntisDate(data.startDate),
endDate: Base.convertUntisDate(data.endDate),
};
}

/**
* Convert a JS Date Object to a WebUntis date string
* @param {Date} date
Expand Down

0 comments on commit e801775

Please sign in to comment.