Skip to content

Commit

Permalink
fixing fetches, linted
Browse files Browse the repository at this point in the history
  • Loading branch information
DimitriB01 committed Sep 12, 2024
1 parent 4f86daf commit e22b4d7
Show file tree
Hide file tree
Showing 15 changed files with 65 additions and 45 deletions.
4 changes: 2 additions & 2 deletions src/common/hooks/University/University.hooks.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import log from 'loglevel'
import { usePersistedStore } from '@store'
import { useEffect, useMemo, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { useState, useEffect, useMemo } from 'react'
import { usePersistedStore } from '@store'

export type UniversityHookReturn = {
readonly university: string
Expand Down
1 change: 1 addition & 0 deletions src/common/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { HaskiTheme } from './Theme/HaskiTheme'
import { Theme } from './Theme/Theme'
import { reportWebVitals, sendToAnalytics } from './Webvitals/Webvitals'

export { reportWebVitals, sendToAnalytics, Theme, HaskiTheme }
8 changes: 4 additions & 4 deletions src/components/Newsbanner/Newsbanner.hooks.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import i18next from 'i18next'
import log from 'loglevel'
import { useMemo, useState, useContext, useEffect } from 'react'
import { SnackbarContext } from '@services'
import { useSessionStore } from '@store'
import { useContext, useEffect, useMemo, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { useUniversity } from '@common/hooks'
import i18next from 'i18next'
import { SnackbarContext } from '@services'
import { useSessionStore } from '@store'

/**
* @prop sets the newsItem if there is atleast one news
Expand Down
4 changes: 2 additions & 2 deletions src/components/Newsbanner/Newsbanner.test.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import '@testing-library/jest-dom'
import { fireEvent, render, act, renderHook } from '@testing-library/react'
import { act, fireEvent, render, renderHook } from '@testing-library/react'
import { mockServices } from 'jest.setup'
import Newsbanner from './Newsbanner'
import { MemoryRouter } from 'react-router-dom'
import { useUniversity } from '@common/hooks'
import Newsbanner from './Newsbanner'

describe('Newsbanner tests', () => {
beforeEach(() => {
Expand Down
6 changes: 3 additions & 3 deletions src/components/Newsbanner/Newsbanner.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { keyframes } from '@emotion/react'
import { memo } from 'react'
import { Alert, Box, Collapse, IconButton, Typography } from '@common/components'
import { Close } from '@common/icons'
import { memo } from 'react'
import { keyframes } from '@emotion/react'
import { NewsbannerHookReturn, useNewsbanner as _useNewsbanner } from './Newsbanner.hooks'
import { useSessionStore } from '@store'
import { NewsbannerHookReturn, useNewsbanner as _useNewsbanner } from './Newsbanner.hooks'

export type NewsbannerProps = {
useNewsbanner?: () => NewsbannerHookReturn
Expand Down
2 changes: 1 addition & 1 deletion src/components/PrivacyModal/PrivacyModal.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import '@testing-library/jest-dom'
import { fireEvent, render, act } from '@testing-library/react'
import { act, fireEvent, render } from '@testing-library/react'
import { mockServices } from 'jest.setup'
import * as router from 'react-router'
import { MemoryRouter } from 'react-router-dom'
Expand Down
2 changes: 1 addition & 1 deletion src/components/PrivacyModal/PrivacyModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import {
Tooltip,
Typography
} from '@common/components'
import { PrivacyModalHookReturn, usePrivacyModal as _usePrivacyModal } from './PrivacyModal.hooks'
import { useUniversity } from '@common/hooks'
import { PrivacyModalHookReturn, usePrivacyModal as _usePrivacyModal } from './PrivacyModal.hooks'

const style = {
position: 'absolute',
Expand Down
2 changes: 1 addition & 1 deletion src/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ import LearningPathElement from './LearningPathElement/LearningPathElement'
import LearningPathElementStatus from './LearningPathElement/LearningPathElementStatus'
import LearningPathLearningElement from './LearningPathLearningElement/LearningPathLearningElement'
import LearningPathTopic from './LearningPathTopic/LearningPathTopic'
import News, { NewsResponse } from './News/News'
import ILS from './QuestionnaireResults/ILS'
import ListK from './QuestionnaireResults/ListK'
import RemoteCourse from './RemoteCourse/RemoteCourse'
import RemoteLearningElement from './RemoteLearningElement/RemoteLearningElement'
import RemoteTopic from './RemoteTopic/RemoteTopic'
import News, { NewsResponse } from './News/News'
import StudentLearningElement from './StudentLearningElement/StudentLearningElement'
import Topic from './Topic/Topic'
import User from './User/User'
Expand Down
2 changes: 1 addition & 1 deletion src/pages/Contact/Contact.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState, useContext } from 'react'
import { useContext, useState } from 'react'
import { ContactForm } from '@components'
import { AuthContext } from '@services'
import { ContactHookProps, ContactHookReturn, useContact as _useContact } from './Contact.hooks'
Expand Down
4 changes: 2 additions & 2 deletions src/pages/MainFrame/MainFrame.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ import {
Footer,
LocalNavBar,
MenuBar,
Newsbanner,
OpenQuestionnaire,
PrivacyModal,
Newsbanner
PrivacyModal
} from '@components'

/**
Expand Down
2 changes: 1 addition & 1 deletion src/services/News/fetchNews.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NewsResponse } from '@core'
import { getConfig } from '@shared'
import { fetchData } from '../RequestResponse'
import { NewsResponse } from '@core'

/**
*
Expand Down
55 changes: 30 additions & 25 deletions src/services/RemoteCourses/fetchRemoteCourses.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,27 @@ global.fetch = jest.fn(() =>

describe('fetchCourses has expected behaviour', () => {
it('should return the course when the response is successful', async () => {
const expectedData = { course: 'dude where is my car' }
const expectedData = [
{
enddate: 1702166400,
fullname: 'Kurs-1',
id: 2,
shortname: 'kurs',
startdate: 1670630400,
timecreated: 1670578503,
timemodified: 1670578503
},
{
enddate: 1718406000,
fullname: 'Kurs-2',
id: 3,
shortname: 'ku2',
startdate: 1686870000,
timecreated: 1686830366,
timemodified: 1692021711
}
]

const mockResponse = {
ok: true,
json: jest.fn().mockResolvedValue(expectedData)
Expand All @@ -23,22 +43,15 @@ describe('fetchCourses has expected behaviour', () => {
// @ts-ignore
fetch.mockResolvedValue(mockResponse)

const userId = 1
const lmsUserId = 1
const studentId = 1
const result = await fetchRemoteCourses()

const result = await fetchRemoteCourses(userId, lmsUserId, studentId)

expect(fetch).toHaveBeenCalledWith(
`${getConfig().BACKEND}/user/${userId}/${lmsUserId}/student/${studentId}/course`,
{
method: 'GET',
credentials: 'include',
headers: {
'Content-Type': 'application/json'
}
expect(fetch).toHaveBeenCalledWith(`${getConfig().BACKEND}/lms/remote/courses`, {
method: 'GET',
credentials: 'include',
headers: {
'Content-Type': 'application/json'
}
)
})
expect(result).toEqual(expectedData)
})

Expand All @@ -55,11 +68,7 @@ describe('fetchCourses has expected behaviour', () => {
// @ts-ignore
fetch.mockResolvedValue(mockResponse)

const userId = 1
const lmsUserId = 1
const studentId = 1

await expect(fetchRemoteCourses(userId, lmsUserId, studentId)).rejects.toThrow(`${expectedMessage}`)
await expect(fetchRemoteCourses()).rejects.toThrow(`${expectedMessage}`)
})

it('should throw an unknown error when the response does not have an error variable', async () => {
Expand All @@ -72,10 +81,6 @@ describe('fetchCourses has expected behaviour', () => {
// @ts-ignore
fetch.mockResolvedValue(mockResponse)

const userId = 1
const lmsUserId = 1
const studentId = 1

await expect(fetchRemoteCourses(userId, lmsUserId, studentId)).rejects.toThrow('')
await expect(fetchRemoteCourses()).rejects.toThrow('')
})
})
7 changes: 7 additions & 0 deletions src/services/RemoteCourses/fetchRemoteCourses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@ import { RemoteCourse } from '@core'
import { getConfig } from '@shared'
import { fetchData } from '../RequestResponse'

/*
* fetchRemoteCourses function.
*
* @returns - The remote courses array from LMS
*
* @category Services
*/
export const fetchRemoteCourses = async () => {
return fetchData<RemoteCourse[]>(getConfig().BACKEND + `/lms/remote/courses`, {
method: 'GET',
Expand Down
9 changes: 8 additions & 1 deletion src/services/RemoteTopics/fetchRemoteTopics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import { getConfig } from '@shared'
import RemoteTopic, { RemoteTopicReturn } from '../../core/RemoteTopic/RemoteTopic'
import { fetchData } from '../RequestResponse'

export const fetchRemoteTopics: RemoteTopicReturn = async (courseId: number) => {
/*
* fetchRemoteTopics function.
*
* @returns - The remote topics and learning eleemnts array from LMS
*
* @category Services
*/
export const fetchRemoteTopics: RemoteTopicReturn = async (courseId?: string) => {
return fetchData<RemoteTopic[]>(getConfig().BACKEND + `/lms/remote/course/${courseId}/content`, {
method: 'GET',
credentials: 'include',
Expand Down
2 changes: 1 addition & 1 deletion src/store/Zustand/Store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ import LearningPathElementStatusSlice, {
createLearningPathElementStatusSlice
} from '../Slices/LearningPathElementStatusSlice'
import LearningPathTopicSlice, { createLearningPathTopicSlice } from '../Slices/LearningPathTopicSlice'
import NewsSlice, { createNewsSlice } from '../Slices/NewsSlice'
import RemoteTopicSlice, { createRemoteTopicSlice } from '../Slices/RemoteTopicSlice'
import UserSlice, { createUserSlice } from '../Slices/UserSlice'
import NewsSlice, { createNewsSlice } from '../Slices/NewsSlice'
import xAPISlice, { createXAPISlice } from '../Slices/xAPISlice'

export type StoreState = LearningPathElementSlice &
Expand Down

0 comments on commit e22b4d7

Please sign in to comment.