-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
issues/KUI-1176 syllabus bugfix #345
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,35 @@ | ||
import React from 'react' | ||
import { Col } from 'reactstrap' | ||
import { FaAsterisk } from 'react-icons/fa' | ||
import { useLanguage } from '../hooks/useLanguage' | ||
|
||
const SyllabusInformation = ({ context = {}, syllabus = {}, hasSyllabus, translation = {} }) => { | ||
const { courseCode } = context | ||
const ActualSyllabusInformation = ({ syllabusName }) => { | ||
const { translation } = useLanguage() | ||
|
||
const { courseLabels, courseInformation } = translation | ||
const { | ||
label_course_syllabus_source: labelCourseSyllabusSource, | ||
label_course_syllabus_denoted: labelCourseSyllabusDenoted, | ||
syllabus_marker_aria_label: labelCourseSyllabusAsterisk, | ||
} = courseLabels | ||
const { course_short_semester: courseShortSemester } = courseInformation | ||
|
||
const { course_valid_from: courseValidFrom = ['', ''], course_valid_to: courseValidTo = ['', ''] } = syllabus | ||
const courseValidFromLabel = `${courseShortSemester[courseValidFrom[1]]}${courseValidFrom[0]}` | ||
const courseValidToLabel = courseValidTo.length ? courseShortSemester[courseValidTo[1]] + '' + courseValidTo[0] : '' | ||
const courseValidRangeLabel = `(${courseValidFromLabel}\u2013${courseValidToLabel})` | ||
|
||
const syllabusText = `${labelCourseSyllabusSource} ${courseCode} ${courseValidRangeLabel} ${labelCourseSyllabusDenoted}` | ||
} = translation.courseLabels | ||
|
||
return ( | ||
<Col sm="12"> | ||
<div> | ||
{hasSyllabus && ( | ||
<span> | ||
{syllabusText} | ||
{' ( '} | ||
<FaAsterisk className="syllabus-marker-icon-small" aria-label={labelCourseSyllabusAsterisk} /> | ||
{' )'} | ||
</span> | ||
)} | ||
<span> | ||
{`${labelCourseSyllabusSource} ${syllabusName} ${labelCourseSyllabusDenoted}`} | ||
{' ( '} | ||
<FaAsterisk className="syllabus-marker-icon-small" aria-label={labelCourseSyllabusAsterisk} /> | ||
{' )'} | ||
</span> | ||
</div> | ||
</Col> | ||
) | ||
} | ||
|
||
export default SyllabusInformation | ||
const SyllabusInformationOrNull = ({ syllabusName }) => { | ||
if (!syllabusName) return null | ||
|
||
return <ActualSyllabusInformation syllabusName={syllabusName} /> | ||
} | ||
|
||
export { SyllabusInformationOrNull as SyllabusInformation } | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 29 additions & 10 deletions
39
public/js/app/components/__tests__/SyllabusInformation.test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,35 @@ | ||
import React from 'react' | ||
import { render } from '@testing-library/react' | ||
import { render, screen } from '@testing-library/react' | ||
import '@testing-library/jest-dom' | ||
|
||
import SyllabusInformation from '../SyllabusInformation' | ||
import i18n from '../../../../../i18n' | ||
|
||
const [translationEN, translationSV] = i18n.messages | ||
import { SyllabusInformation } from '../SyllabusInformation' | ||
import { WebContextProvider } from '../../context/WebContext' | ||
|
||
describe('Component <SyllabusInformation>', () => { | ||
test('renders syllabus information in English', () => { | ||
render(<SyllabusInformation translation={translationEN} />) | ||
}) | ||
test('renders syllabus information in Swedish', () => { | ||
render(<SyllabusInformation translation={translationSV} />) | ||
test.each(['AF1745 (Spring 2023–)', 'SH2020 (Autumn 2022-Spring 2024'])( | ||
`renders syllabus information if SyllabusName "%s" is given`, | ||
syllabusName => { | ||
render( | ||
<WebContextProvider configIn={{ lang: 'en' }}> | ||
<SyllabusInformation syllabusName={syllabusName} /> | ||
</WebContextProvider> | ||
) | ||
|
||
expect( | ||
screen.getByText( | ||
`Headings with content from the Course syllabus ${syllabusName} are denoted with an asterisk ( )` | ||
) | ||
).toBeInTheDocument() | ||
} | ||
) | ||
|
||
test('does not render syllabus information if no syllabusName is given', () => { | ||
render( | ||
<WebContextProvider configIn={{ lang: 'en' }}> | ||
<SyllabusInformation syllabusName={''} /> | ||
</WebContextProvider> | ||
) | ||
|
||
expect(screen.queryByText(`Headings with content from the Course syllabus`)).not.toBeInTheDocument() | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious what you think about this solution
I thought that calling the component
SyllabusInformationOrNull
forSyllabusInformation
would be misleading. Rather the componentActualSyllabusInformation
should be calledSyllabusInformation
, but then we would have a mismatch in file name <-> exported component.Note also the change to named export.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First: I like the change to named export. What are the real advantages of using default export? Or why don't we always use named exports?
I'm not sure I think calling
SyllabusInformationOrNull
forSyllabusInformation
would be misleading actually. For the "user"SyllabusInformation
is what the component actually is. The null check with an extra component is just an internal behaviour the "user" doesn't need to know about ("user"=the other component using this component).And with that said, the only like important thing is the name of the actual exported component.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just curious about the approach here.
Why can’t the functional component ActualSyllabusInformation just be called SyllabusInformation and either just return the html if syllabusName exists or return (null) if syllabusName does not exist? Is it not possible to get rid of SyllabusInformationOrNull altogether?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very good question!
This is certainly possible. This is a rather philosophical discussion about readability and the notion that "one function should do one thing", which - I think - is mentioned in
Clean Code
in the chapter about functions.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I completely agree with Karl and think the approach that you have used is indeed the cleanest approach that could be used here.
right now there is nothing that mislead other developers. I also really liked the aliasing approach for exporting the component.
I used to handle this functionality (checking to show the component if is not null) inline in the other components and now I think this approach can help me to have cleaner code.
I'll also check the mentioned part inside the clean code book which is valuable to read.