Skip to content

Commit

Permalink
Merge pull request #63 from flacial/62-unable-to-submit-an-html-file-…
Browse files Browse the repository at this point in the history
…to-js3

fix: Unable to submit an HTML file to JS3
  • Loading branch information
flacial authored Oct 8, 2022
2 parents ca3de47 + 41e3655 commit 74c0474
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/@types/prompt.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export type AskForChallenges = (
) => Promise<{
lessonId: number
challengeId: number
lessonOrder: number
lessonOrder: string
challengeOrder: number
}>

Expand Down
2 changes: 1 addition & 1 deletion src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ please either create an issue on ${bold.magenta(
)}\n`

export const INVALID_CHALLENGE_FILE = bold.red(
'A valid challenge file could not be found. Make sure the file is not renamed and it has the following format: 1.js, 2.js, 3.js, ...etc\n'
'A valid challenge file could not be found. Make sure the file is not renamed and it has the following format: 1.js, 2.js, 3.js, 10.html (only for JS3 challenge 10) ...etc\n'
)

export const PROMPT_ORDER = bold.red(`The number needs to be a non-negative integer.
Expand Down
4 changes: 2 additions & 2 deletions src/util/dynamicMessages.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import { bold } from 'chalk'
describe('Dynamic messages', () => {
it('Should add string to INVALID_SECOND_FILE', () => {
expect(INVALID_SECOND_FILE('abc.js')).toEqual(
bold.red("Invalid second file (abc.js) to be submitted. Please make sure you are submitting a valid test file (abc.test.js) if it is supposed to be a test file, else remove it and submit")
bold.red("Invalid second file (abc.js) to be submitted. Please make sure you are submitting a valid test file (abc.test.js) if it is supposed to be a test file.\nIf it's supposed to be the HTML script file, please include it in the HTML file.")
)
})

it('Should remove parent dir and add string to INVALID_SECOND_FILE', () => {
expect(INVALID_SECOND_FILE('abc')).toEqual(
bold.red("Invalid second file (abc) to be submitted. Please make sure you are submitting a valid test file (abc.test.js) if it is supposed to be a test file, else remove it and submit")
bold.red("Invalid second file (abc) to be submitted. Please make sure you are submitting a valid test file (abc.test.js) if it is supposed to be a test file.\nIf it's supposed to be the HTML script file, please include it in the HTML file.")
)
})
})
2 changes: 1 addition & 1 deletion src/util/dynamicMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const INVALID_SECOND_FILE = (invalidFile?: string): string =>
`(${
invalidFile.includes('.') ? invalidFile.split('.')[0] : invalidFile
}.test.js)`
} if it is supposed to be a test file, else remove it and submit`
} if it is supposed to be a test file.\nIf it's supposed to be the HTML script file, please include it in the HTML file.`
)

export const WRONG_CHALLENGE_TO_SUBMIT = (
Expand Down
73 changes: 58 additions & 15 deletions src/util/git.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import {
INVALID_CHALLENGE_FILE,
SUBMITTING_PLUS_TWO_FILES,
} from '../messages'
import { INVALID_SECOND_FILE, WRONG_CHALLENGE_TO_SUBMIT } from './dynamicMessages'
import {
INVALID_SECOND_FILE,
WRONG_CHALLENGE_TO_SUBMIT,
} from './dynamicMessages'

jest.mock('simple-git/promise', () =>
jest.fn(() => {
Expand All @@ -27,6 +30,7 @@ jest.mock('simple-git/promise', () =>
.mockResolvedValueOnce({ current: 'notMaster' })
.mockResolvedValueOnce({ current: 'notMaster' })
.mockResolvedValueOnce({ current: 'notMaster' })
.mockResolvedValueOnce({ current: 'notMaster' })
.mockResolvedValueOnce({ current: 'notMaster' }),

diff: jest
Expand Down Expand Up @@ -68,18 +72,31 @@ jest.mock('simple-git/promise', () =>
// Should not throw error: INVALID_CHALLENGE_FILE on wrong file
.mockResolvedValueOnce('\njs0/1.js')
.mockResolvedValueOnce('\njs0/1.js')
.mockResolvedValueOnce('\njs0/1.js'),
.mockResolvedValueOnce('\njs0/1.js')
// Should submit if html file and lesson is JS3
.mockResolvedValueOnce('\njs3/1.html')
.mockResolvedValueOnce('\njs3/1.html')
.mockResolvedValueOnce('\njs3/1.html')
// Should continue if not valid html file
.mockResolvedValueOnce('\njs3/1.mdx')
.mockResolvedValueOnce('\njs3/1.mdx')
.mockResolvedValueOnce('\njs3/1.mdx'),

raw: jest
.fn()
.mockResolvedValueOnce(`
.mockResolvedValueOnce(
`
7abfefd HEAD@{0}: checkout: moving from master to branch2
7abfefd HEAD@{1}: checkout: moving from branch2 to master
`)
.mockResolvedValueOnce(`
`
)
.mockResolvedValueOnce(
`
7abfefd HEAD@{0}: checkout: moving from branch3 to branch2
7abfefd HEAD@{1}: checkout: moving from branch2 to master
`)
`
)
.mockResolvedValueOnce(``)
.mockResolvedValueOnce(``)
.mockResolvedValueOnce(``)
.mockResolvedValueOnce(``)
Expand All @@ -90,6 +107,7 @@ jest.mock('simple-git/promise', () =>
.mockResolvedValueOnce(``)
.mockResolvedValueOnce(``)
.mockResolvedValueOnce(``)
.mockResolvedValueOnce(``),
}
})
)
Expand Down Expand Up @@ -125,7 +143,7 @@ describe('getDiffAgainstMaster', () => {
test('Should log: FAILED_GET_LASTCHECKOUT', (done) => {
expect.assertions(1)

const consoleSpy = jest.spyOn(console, 'log');
const consoleSpy = jest.spyOn(console, 'log')

// Assertions become 2 if used await
getDiffAgainstMaster(5, 1).then(() => {
Expand All @@ -139,19 +157,25 @@ describe('getDiffAgainstMaster', () => {
test('Should throw error: SUBMITTING_PLUS_TWO_FILES', () => {
expect.assertions(1)

return expect(getDiffAgainstMaster(2, 1)).rejects.toThrow(SUBMITTING_PLUS_TWO_FILES)
return expect(getDiffAgainstMaster(2, 1)).rejects.toThrow(
SUBMITTING_PLUS_TWO_FILES
)
})

test('Should throw error: INVALID_CHALLENGE_FILE for no challenge files', () => {
expect.assertions(1)

return expect(getDiffAgainstMaster(2, 1)).rejects.toThrow(INVALID_CHALLENGE_FILE)
return expect(getDiffAgainstMaster(2, 1)).rejects.toThrow(
INVALID_CHALLENGE_FILE
)
})

test('Should throw error: SUBMITTING_PLUS_TWO_FILES for submitting +2 challenges', () => {
expect.assertions(1)

return expect(getDiffAgainstMaster(2, 1)).rejects.toThrow(SUBMITTING_PLUS_TWO_FILES)
return expect(getDiffAgainstMaster(2, 1)).rejects.toThrow(
SUBMITTING_PLUS_TWO_FILES
)
})

test('Should throw error: INVALID_SECOND_FILE', () => {
Expand All @@ -167,8 +191,8 @@ describe('getDiffAgainstMaster', () => {
expect.assertions(1)

return expect(getDiffAgainstMaster(2, 1)).resolves.toStrictEqual({
db: "\njs0/1.js\njs0/1.test.js",
display: "\njs0/1.js\njs0/1.test.js",
db: '\njs0/1.js\njs0/1.test.js',
display: '\njs0/1.js\njs0/1.test.js',
})
})

Expand All @@ -184,15 +208,34 @@ describe('getDiffAgainstMaster', () => {
test('Should throw error: INVALID_CHALLENGE_FILE on wrong file', () => {
expect.assertions(1)

return expect(getDiffAgainstMaster(2, 1)).rejects.toThrow(INVALID_CHALLENGE_FILE)
return expect(getDiffAgainstMaster(2, 1)).rejects.toThrow(
INVALID_CHALLENGE_FILE
)
})

test('Should not throw error: INVALID_CHALLENGE_FILE on wrong file', () => {
expect.assertions(1)

return expect(getDiffAgainstMaster(2, 1)).resolves.toStrictEqual({
db: "\njs0/1.js",
display: "\njs0/1.js",
db: '\njs0/1.js',
display: '\njs0/1.js',
})
})

test('Should submit if html file and lesson is JS3', () => {
expect.assertions(1)

return expect(getDiffAgainstMaster(3, 10)).resolves.toStrictEqual({
db: '\njs3/1.html',
display: '\njs3/1.html',
})
})

test('Should continue if not valid html file', () => {
expect.assertions(1)

return expect(getDiffAgainstMaster(3, 10)).rejects.toThrow(
INVALID_CHALLENGE_FILE
)
})
})
14 changes: 11 additions & 3 deletions src/util/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,16 @@ const predictValidFile = (matchWith: string | RegExp) => (e: string) =>

const validateFiles = (
changedFilesString: string,
lessonOrder: number,
lessonOrder: string,
selectedChallengeOrder: string
) => {
// 3 is js3 or Objects
if (lessonOrder > 3) return
if (+lessonOrder > 3) return

const fileNameRegex = /(^\d+).js/g // Matches 1.js 2.js 3.js ...etc
const fileNameWithHtmlRegex = /(.+).html/g // Matches *.html
const predictCorrectFileName = predictValidFile(fileNameRegex)
const predictCorrectFileNameWithHtml = predictValidFile(fileNameWithHtmlRegex)

const changedFilesArray = changedFilesString.trim().split('\n')

Expand All @@ -62,6 +64,12 @@ const validateFiles = (
const isFileValid = predictCorrectFileName(challengeFile)
const changedFileOrder = challengeFile.split('.')[0].split('/')[1]

if (+lessonOrder === 3) {
const validHtmlFile = predictCorrectFileNameWithHtml(challengeFile)

if (validHtmlFile) return
}

if (!isFileValid) throw new Error(INVALID_CHALLENGE_FILE)

// If the challenge to submit is not equal to the modified challenge file
Expand Down Expand Up @@ -103,7 +111,7 @@ const validateFiles = (
}

export const getDiffAgainstMaster = async (
lessonOrder: number,
lessonOrder: string,
challengeOrder: number
): Promise<DiffObject> => {
const { current } = await git.branch()
Expand Down
2 changes: 1 addition & 1 deletion src/util/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const getOptionDisplayStr: GetOptionDisplayStr = (array) => {
export const askForChallenges: AskForChallenges = async (lessons) => {
const lessonsByOrder = getMapFromOptions(lessons)
displayBoxUI(getOptionDisplayStr(lessons).trimEnd())
const { lessonOrder }: { lessonOrder: number } = await prompt([
const { lessonOrder }: { lessonOrder: string } = await prompt([
{
type: 'input',
name: 'lessonOrder',
Expand Down

0 comments on commit 74c0474

Please sign in to comment.