Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
463f51d6
Commit
463f51d6
authored
Jul 13, 2021
by
Himanshu Kapoor
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add tests for the insert image toolbar button
parent
359961ca
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
105 additions
and
12 deletions
+105
-12
app/assets/javascripts/content_editor/components/toolbar_image_button.vue
...cripts/content_editor/components/toolbar_image_button.vue
+27
-12
spec/frontend/content_editor/components/toolbar_image_button_spec.js
...nd/content_editor/components/toolbar_image_button_spec.js
+78
-0
No files found.
app/assets/javascripts/content_editor/components/toolbar_image_button.vue
View file @
463f51d6
...
@@ -36,25 +36,40 @@ export default {
...
@@ -36,25 +36,40 @@ export default {
};
};
},
},
methods
:
{
methods
:
{
clean
Fields
()
{
reset
Fields
()
{
this
.
imgSrc
=
''
;
this
.
imgSrc
=
''
;
this
.
$refs
.
file
Upload
.
value
=
''
;
this
.
$refs
.
file
Selector
.
value
=
''
;
},
},
insertImage
()
{
insertImage
()
{
const
alt
=
getImageAlt
(
this
.
imgSrc
);
this
.
tiptapEditor
this
.
tiptapEditor
.
chain
().
focus
().
setImage
({
src
:
this
.
imgSrc
,
alt
}).
run
();
.
chain
()
this
.
cleanFields
();
.
focus
()
.
setImage
({
src
:
this
.
imgSrc
,
canonicalSrc
:
this
.
imgSrc
,
alt
:
getImageAlt
(
this
.
imgSrc
),
})
.
run
();
this
.
resetFields
();
this
.
emitExecute
();
this
.
emitExecute
();
},
},
emitExecute
(
source
=
'
url
'
)
{
emitExecute
(
source
=
'
url
'
)
{
this
.
$emit
(
'
execute
'
,
{
contentType
:
'
image
'
,
value
:
source
});
this
.
$emit
(
'
execute
'
,
{
contentType
:
'
image
'
,
value
:
source
});
},
},
openFileUpload
()
{
openFileUpload
()
{
this
.
$refs
.
file
Upload
.
click
();
this
.
$refs
.
file
Selector
.
click
();
},
},
onUploadChange
(
e
)
{
onFileSelect
(
e
)
{
this
.
tiptapEditor
.
chain
().
focus
().
uploadImage
({
file
:
e
.
target
.
files
[
0
]
}).
run
();
this
.
tiptapEditor
this
.
cleanFields
();
.
chain
()
.
focus
()
.
uploadImage
({
file
:
e
.
target
.
files
[
0
],
})
.
run
();
this
.
resetFields
();
this
.
emitExecute
(
'
upload
'
);
this
.
emitExecute
(
'
upload
'
);
},
},
},
},
...
@@ -69,7 +84,7 @@ export default {
...
@@ -69,7 +84,7 @@ export default {
size=
"small"
size=
"small"
category=
"tertiary"
category=
"tertiary"
icon=
"media"
icon=
"media"
@
hidden=
"
clean
Fields()"
@
hidden=
"
reset
Fields()"
>
>
<gl-dropdown-form
class=
"gl-px-3!"
>
<gl-dropdown-form
class=
"gl-px-3!"
>
<gl-form-input-group
v-model=
"imgSrc"
:placeholder=
"__('Image URL')"
>
<gl-form-input-group
v-model=
"imgSrc"
:placeholder=
"__('Image URL')"
>
...
@@ -84,12 +99,12 @@ export default {
...
@@ -84,12 +99,12 @@ export default {
</gl-dropdown-item>
</gl-dropdown-item>
<input
<input
ref=
"file
Upload
"
ref=
"file
Selector
"
type=
"file"
type=
"file"
name=
"content_editor_image"
name=
"content_editor_image"
:accept=
"$options.acceptedMimes"
:accept=
"$options.acceptedMimes"
class=
"gl-display-none"
class=
"gl-display-none"
@
change=
"on
UploadChange
"
@
change=
"on
FileSelect
"
/>
/>
</gl-dropdown>
</gl-dropdown>
</template>
</template>
spec/frontend/content_editor/components/toolbar_image_button_spec.js
0 → 100644
View file @
463f51d6
import
{
GlButton
,
GlFormInputGroup
}
from
'
@gitlab/ui
'
;
import
{
mountExtended
}
from
'
helpers/vue_test_utils_helper
'
;
import
ToolbarImageButton
from
'
~/content_editor/components/toolbar_image_button.vue
'
;
import
{
configure
as
configureImageExtension
}
from
'
~/content_editor/extensions/image
'
;
import
{
createTestEditor
,
mockChainedCommands
}
from
'
../test_utils
'
;
describe
(
'
content_editor/components/toolbar_image_button
'
,
()
=>
{
let
wrapper
;
let
editor
;
const
buildWrapper
=
()
=>
{
wrapper
=
mountExtended
(
ToolbarImageButton
,
{
propsData
:
{
tiptapEditor
:
editor
,
},
});
};
const
findImageURLInput
=
()
=>
wrapper
.
findComponent
(
GlFormInputGroup
).
find
(
'
input[type="text"]
'
);
const
findApplyImageButton
=
()
=>
wrapper
.
findComponent
(
GlButton
);
const
selectFile
=
async
(
file
)
=>
{
const
input
=
wrapper
.
find
({
ref
:
'
fileSelector
'
});
// override the property definition because `input.files` isn't directly modifyable
Object
.
defineProperty
(
input
.
element
,
'
files
'
,
{
value
:
[
file
],
writable
:
true
});
await
input
.
trigger
(
'
change
'
);
};
beforeEach
(()
=>
{
const
{
tiptapExtension
:
Image
}
=
configureImageExtension
({
renderMarkdown
:
jest
.
fn
(),
uploadsPath
:
'
/uploads/
'
,
});
editor
=
createTestEditor
({
extensions
:
[
Image
],
});
buildWrapper
();
});
afterEach
(()
=>
{
editor
.
destroy
();
wrapper
.
destroy
();
});
it
(
'
sets the image to the value in the URL input when "Insert" button is clicked
'
,
async
()
=>
{
const
commands
=
mockChainedCommands
(
editor
,
[
'
focus
'
,
'
setImage
'
,
'
run
'
]);
await
findImageURLInput
().
setValue
(
'
https://example.com/img.jpg
'
);
await
findApplyImageButton
().
trigger
(
'
click
'
);
expect
(
commands
.
focus
).
toHaveBeenCalled
();
expect
(
commands
.
setImage
).
toHaveBeenCalledWith
({
alt
:
'
img
'
,
src
:
'
https://example.com/img.jpg
'
,
canonicalSrc
:
'
https://example.com/img.jpg
'
,
});
expect
(
commands
.
run
).
toHaveBeenCalled
();
expect
(
wrapper
.
emitted
().
execute
[
0
]).
toEqual
([{
contentType
:
'
image
'
,
value
:
'
url
'
}]);
});
it
(
'
uploads the selected image when file input changes
'
,
async
()
=>
{
const
commands
=
mockChainedCommands
(
editor
,
[
'
focus
'
,
'
uploadImage
'
,
'
run
'
]);
const
file
=
new
File
([
'
foo
'
],
'
foo.png
'
,
{
type
:
'
image/png
'
});
await
selectFile
(
file
);
expect
(
commands
.
focus
).
toHaveBeenCalled
();
expect
(
commands
.
uploadImage
).
toHaveBeenCalledWith
({
file
});
expect
(
commands
.
run
).
toHaveBeenCalled
();
expect
(
wrapper
.
emitted
().
execute
[
0
]).
toEqual
([{
contentType
:
'
image
'
,
value
:
'
upload
'
}]);
});
});
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment