-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
[WIP] feat: commit media with post #2823
Closed
Closed
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
85dd832
feat: commit media with post - initial commit
erezrokah b30dfec
feat: add draft media indication
erezrokah 283e763
feat: sync UI media files with GitHub on entry load
erezrokah c91eff3
feat: bug fixes
erezrokah 2a22fbb
feat: delete media files from github when removed from library
erezrokah 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
118 changes: 118 additions & 0 deletions
118
packages/netlify-cms-backend-github/src/__tests__/implementation.spec.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 |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import GitHubImplementation from '../implementation'; | ||
|
||
describe('github backend implementation', () => { | ||
describe('persistMedia', () => { | ||
jest.spyOn(console, 'error').mockImplementation(() => {}); | ||
|
||
const persistFiles = jest.fn(); | ||
const mockAPI = { | ||
persistFiles, | ||
}; | ||
|
||
persistFiles.mockImplementation((_, files) => { | ||
files.forEach((file, index) => { | ||
file.sha = index; | ||
}); | ||
}); | ||
|
||
const createObjectURL = jest.fn(); | ||
global.URL = { | ||
createObjectURL, | ||
}; | ||
|
||
createObjectURL.mockReturnValue('displayURL'); | ||
|
||
const config = { | ||
getIn: jest.fn().mockImplementation(array => { | ||
if (array[0] === 'backend' && array[1] === 'repo') { | ||
return 'owner/repo'; | ||
} | ||
if (array[0] === 'backend' && array[1] === 'open_authoring') { | ||
return false; | ||
} | ||
if (array[0] === 'backend' && array[1] === 'branch') { | ||
return 'master'; | ||
} | ||
}), | ||
}; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should persist media file when not draft', async () => { | ||
const gitHubImplementation = new GitHubImplementation(config); | ||
gitHubImplementation.api = mockAPI; | ||
|
||
const mediaFile = { | ||
value: 'image.png', | ||
fileObj: { size: 100 }, | ||
path: '/media/image.png', | ||
}; | ||
|
||
expect.assertions(5); | ||
await expect(gitHubImplementation.persistMedia(mediaFile)).resolves.toEqual({ | ||
id: 0, | ||
name: 'image.png', | ||
size: 100, | ||
displayURL: 'displayURL', | ||
path: 'media/image.png', | ||
draft: undefined, | ||
}); | ||
|
||
expect(persistFiles).toHaveBeenCalledTimes(1); | ||
expect(persistFiles).toHaveBeenCalledWith(null, [mediaFile], {}); | ||
expect(createObjectURL).toHaveBeenCalledTimes(1); | ||
expect(createObjectURL).toHaveBeenCalledWith(mediaFile.fileObj); | ||
}); | ||
|
||
it('should not persist media file when draft', async () => { | ||
const gitHubImplementation = new GitHubImplementation(config); | ||
gitHubImplementation.api = mockAPI; | ||
|
||
createObjectURL.mockReturnValue('displayURL'); | ||
|
||
const mediaFile = { | ||
value: 'image.png', | ||
fileObj: { size: 100 }, | ||
path: '/media/image.png', | ||
}; | ||
|
||
expect.assertions(4); | ||
await expect(gitHubImplementation.persistMedia(mediaFile, { draft: true })).resolves.toEqual({ | ||
id: undefined, | ||
name: 'image.png', | ||
size: 100, | ||
displayURL: 'displayURL', | ||
path: 'media/image.png', | ||
draft: true, | ||
}); | ||
|
||
expect(persistFiles).toHaveBeenCalledTimes(0); | ||
expect(createObjectURL).toHaveBeenCalledTimes(1); | ||
expect(createObjectURL).toHaveBeenCalledWith(mediaFile.fileObj); | ||
}); | ||
|
||
it('should log and throw error on "persistFiles" error', async () => { | ||
const gitHubImplementation = new GitHubImplementation(config); | ||
gitHubImplementation.api = mockAPI; | ||
|
||
const error = new Error('failed to persist files'); | ||
persistFiles.mockRejectedValue(error); | ||
|
||
const mediaFile = { | ||
value: 'image.png', | ||
fileObj: { size: 100 }, | ||
path: '/media/image.png', | ||
}; | ||
|
||
expect.assertions(5); | ||
await expect(gitHubImplementation.persistMedia(mediaFile)).rejects.toThrowError(error); | ||
|
||
expect(persistFiles).toHaveBeenCalledTimes(1); | ||
expect(createObjectURL).toHaveBeenCalledTimes(0); | ||
expect(console.error).toHaveBeenCalledTimes(1); | ||
expect(console.error).toHaveBeenCalledWith(error); | ||
}); | ||
}); | ||
}); |
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.
Instead of the re-uploading changes and deleting previously-stored media files, which will still occur if the draft change didn't add any new media file. Why don't we make use of the
uploaded
flag that is set here and then filter the loaded draft media files using this flag to get newly added media file(s), and then push the new media file(s) on draft update and concat here with previous ones.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.
Thanks @barthc I haven't finished the part of deleting media files from the pr. I'll look into the
uploaded
flag.