2020-03-13 15:44:24 +05:30
import { mount } from '@vue/test-utils' ;
2021-03-11 19:13:27 +05:30
import { nextTick } from 'vue' ;
2020-10-24 23:57:45 +05:30
import {
Blob as BlobMock ,
SimpleViewerMock ,
RichViewerMock ,
RichBlobContentMock ,
SimpleBlobContentMock ,
} from 'jest/blob/components/mock_data' ;
2020-03-13 15:44:24 +05:30
import BlobContent from '~/blob/components/blob_content.vue' ;
2021-03-11 19:13:27 +05:30
import BlobHeader from '~/blob/components/blob_header.vue' ;
2020-06-23 00:09:42 +05:30
import {
BLOB _RENDER _EVENT _LOAD ,
BLOB _RENDER _EVENT _SHOW _SOURCE ,
BLOB _RENDER _ERRORS ,
} from '~/blob/components/constants' ;
2021-03-11 19:13:27 +05:30
import SnippetBlobView from '~/snippets/components/snippet_blob_view.vue' ;
2022-10-11 01:57:18 +05:30
import { VISIBILITY _LEVEL _PUBLIC _STRING } from '~/visibility_level/constants' ;
2021-03-11 19:13:27 +05:30
import { RichViewer , SimpleViewer } from '~/vue_shared/components/blob_viewers' ;
2020-03-13 15:44:24 +05:30
describe ( 'Blob Embeddable' , ( ) => {
let wrapper ;
const snippet = {
id : 'gid://foo.bar/snippet' ,
webUrl : 'https://foo.bar' ,
2022-10-11 01:57:18 +05:30
visibilityLevel : VISIBILITY _LEVEL _PUBLIC _STRING ,
2020-03-13 15:44:24 +05:30
} ;
const dataMock = {
activeViewerType : SimpleViewerMock . type ,
} ;
2020-07-28 23:09:34 +05:30
function createComponent ( {
snippetProps = { } ,
data = dataMock ,
blob = BlobMock ,
contentLoading = false ,
} = { } ) {
2020-03-13 15:44:24 +05:30
const $apollo = {
queries : {
blobContent : {
loading : contentLoading ,
2020-05-24 23:13:21 +05:30
refetch : jest . fn ( ) ,
skip : true ,
2020-03-13 15:44:24 +05:30
} ,
} ,
} ;
wrapper = mount ( SnippetBlobView , {
propsData : {
snippet : {
... snippet ,
2020-07-28 23:09:34 +05:30
... snippetProps ,
2020-03-13 15:44:24 +05:30
} ,
2020-07-28 23:09:34 +05:30
blob ,
2020-03-13 15:44:24 +05:30
} ,
data ( ) {
return {
... data ,
} ;
} ,
mocks : { $apollo } ,
} ) ;
}
afterEach ( ( ) => {
wrapper . destroy ( ) ;
} ) ;
describe ( 'rendering' , ( ) => {
it ( 'renders correct components' , ( ) => {
createComponent ( ) ;
2022-11-25 23:54:43 +05:30
expect ( wrapper . findComponent ( BlobHeader ) . exists ( ) ) . toBe ( true ) ;
expect ( wrapper . findComponent ( BlobContent ) . exists ( ) ) . toBe ( true ) ;
2020-03-13 15:44:24 +05:30
} ) ;
it ( 'sets simple viewer correctly' , ( ) => {
createComponent ( ) ;
2022-11-25 23:54:43 +05:30
expect ( wrapper . findComponent ( SimpleViewer ) . exists ( ) ) . toBe ( true ) ;
2020-03-13 15:44:24 +05:30
} ) ;
it ( 'sets rich viewer correctly' , ( ) => {
2020-05-24 23:13:21 +05:30
const data = { ... dataMock , activeViewerType : RichViewerMock . type } ;
2020-07-28 23:09:34 +05:30
createComponent ( {
data ,
} ) ;
2022-11-25 23:54:43 +05:30
expect ( wrapper . findComponent ( RichViewer ) . exists ( ) ) . toBe ( true ) ;
2020-03-13 15:44:24 +05:30
} ) ;
2022-04-04 11:22:00 +05:30
it ( 'correctly switches viewer type' , async ( ) => {
2020-03-13 15:44:24 +05:30
createComponent ( ) ;
2022-11-25 23:54:43 +05:30
expect ( wrapper . findComponent ( SimpleViewer ) . exists ( ) ) . toBe ( true ) ;
2020-03-13 15:44:24 +05:30
wrapper . vm . switchViewer ( RichViewerMock . type ) ;
2022-04-04 11:22:00 +05:30
await nextTick ( ) ;
2022-11-25 23:54:43 +05:30
expect ( wrapper . findComponent ( RichViewer ) . exists ( ) ) . toBe ( true ) ;
2022-04-04 11:22:00 +05:30
await wrapper . vm . switchViewer ( SimpleViewerMock . type ) ;
2022-11-25 23:54:43 +05:30
expect ( wrapper . findComponent ( SimpleViewer ) . exists ( ) ) . toBe ( true ) ;
2020-03-13 15:44:24 +05:30
} ) ;
2020-06-23 00:09:42 +05:30
it ( 'passes information about render error down to blob header' , ( ) => {
createComponent ( {
blob : {
... BlobMock ,
simpleViewer : {
... SimpleViewerMock ,
renderError : BLOB _RENDER _ERRORS . REASONS . COLLAPSED . id ,
} ,
} ,
} ) ;
2022-11-25 23:54:43 +05:30
expect ( wrapper . findComponent ( BlobHeader ) . props ( 'hasRenderError' ) ) . toBe ( true ) ;
2020-06-23 00:09:42 +05:30
} ) ;
2020-10-24 23:57:45 +05:30
describe ( 'bob content in multi-file scenario' , ( ) => {
const SimpleBlobContentMock2 = {
... SimpleBlobContentMock ,
plainData : 'Another Plain Foo' ,
} ;
const RichBlobContentMock2 = {
... SimpleBlobContentMock ,
richData : 'Another Rich Foo' ,
} ;
it . each `
snippetBlobs | description | currentBlob | expectedContent
$ { [ SimpleBlobContentMock ] } | $ { 'one existing textual blob' } | $ { SimpleBlobContentMock } | $ { SimpleBlobContentMock . plainData }
$ { [ RichBlobContentMock ] } | $ { 'one existing rich blob' } | $ { RichBlobContentMock } | $ { RichBlobContentMock . richData }
$ { [ SimpleBlobContentMock , RichBlobContentMock ] } | $ { 'mixed blobs with current textual blob' } | $ { SimpleBlobContentMock } | $ { SimpleBlobContentMock . plainData }
$ { [ SimpleBlobContentMock , RichBlobContentMock ] } | $ { 'mixed blobs with current rich blob' } | $ { RichBlobContentMock } | $ { RichBlobContentMock . richData }
$ { [ SimpleBlobContentMock , SimpleBlobContentMock2 ] } | $ { 'textual blobs with current textual blob' } | $ { SimpleBlobContentMock } | $ { SimpleBlobContentMock . plainData }
$ { [ RichBlobContentMock , RichBlobContentMock2 ] } | $ { 'rich blobs with current rich blob' } | $ { RichBlobContentMock } | $ { RichBlobContentMock . richData }
` (
'renders correct content for $description' ,
async ( { snippetBlobs , currentBlob , expectedContent } ) => {
const apolloData = {
snippets : {
2021-01-03 14:25:43 +05:30
nodes : [
2020-10-24 23:57:45 +05:30
{
2021-01-03 14:25:43 +05:30
blobs : {
nodes : snippetBlobs ,
2020-10-24 23:57:45 +05:30
} ,
} ,
] ,
} ,
} ;
createComponent ( {
blob : {
... BlobMock ,
path : currentBlob . path ,
} ,
} ) ;
// mimic apollo's update
2022-03-02 08:16:31 +05:30
// setData usage is discouraged. See https://gitlab.com/groups/gitlab-org/-/epics/7330 for details
// eslint-disable-next-line no-restricted-syntax
2020-10-24 23:57:45 +05:30
wrapper . setData ( {
blobContent : wrapper . vm . onContentUpdate ( apolloData ) ,
} ) ;
await nextTick ( ) ;
2022-11-25 23:54:43 +05:30
const findContent = ( ) => wrapper . findComponent ( BlobContent ) ;
2020-10-24 23:57:45 +05:30
expect ( findContent ( ) . props ( 'content' ) ) . toBe ( expectedContent ) ;
} ,
) ;
} ) ;
2020-03-13 15:44:24 +05:30
describe ( 'URLS with hash' , ( ) => {
afterEach ( ( ) => {
window . location . hash = '' ;
} ) ;
2022-11-25 23:54:43 +05:30
describe ( 'if hash starts with #LC' , ( ) => {
beforeEach ( ( ) => {
window . location . hash = '#LC2' ;
} ) ;
it ( 'renders simple viewer by default' , ( ) => {
createComponent ( {
data : { } ,
} ) ;
expect ( wrapper . vm . activeViewerType ) . toBe ( SimpleViewerMock . type ) ;
expect ( wrapper . findComponent ( SimpleViewer ) . exists ( ) ) . toBe ( true ) ;
2020-07-28 23:09:34 +05:30
} ) ;
2020-03-13 15:44:24 +05:30
2022-11-25 23:54:43 +05:30
describe ( 'switchViewer()' , ( ) => {
it ( 'switches to the passed viewer' , async ( ) => {
createComponent ( ) ;
wrapper . vm . switchViewer ( RichViewerMock . type ) ;
await nextTick ( ) ;
expect ( wrapper . vm . activeViewerType ) . toBe ( RichViewerMock . type ) ;
expect ( wrapper . findComponent ( RichViewer ) . exists ( ) ) . toBe ( true ) ;
await wrapper . vm . switchViewer ( SimpleViewerMock . type ) ;
expect ( wrapper . vm . activeViewerType ) . toBe ( SimpleViewerMock . type ) ;
expect ( wrapper . findComponent ( SimpleViewer ) . exists ( ) ) . toBe ( true ) ;
} ) ;
} ) ;
2020-03-13 15:44:24 +05:30
} ) ;
2022-11-25 23:54:43 +05:30
describe ( 'if hash starts with anything else' , ( ) => {
beforeEach ( ( ) => {
window . location . hash = '#last-headline' ;
} ) ;
2020-03-13 15:44:24 +05:30
2022-11-25 23:54:43 +05:30
it ( 'renders rich viewer by default' , ( ) => {
createComponent ( {
data : { } ,
} ) ;
2022-04-04 11:22:00 +05:30
expect ( wrapper . vm . activeViewerType ) . toBe ( RichViewerMock . type ) ;
2022-11-25 23:54:43 +05:30
expect ( wrapper . findComponent ( RichViewer ) . exists ( ) ) . toBe ( true ) ;
} ) ;
2022-04-04 11:22:00 +05:30
2022-11-25 23:54:43 +05:30
describe ( 'switchViewer()' , ( ) => {
it ( 'switches to the passed viewer' , async ( ) => {
createComponent ( ) ;
wrapper . vm . switchViewer ( SimpleViewerMock . type ) ;
await nextTick ( ) ;
expect ( wrapper . vm . activeViewerType ) . toBe ( SimpleViewerMock . type ) ;
expect ( wrapper . findComponent ( SimpleViewer ) . exists ( ) ) . toBe ( true ) ;
await wrapper . vm . switchViewer ( RichViewerMock . type ) ;
expect ( wrapper . vm . activeViewerType ) . toBe ( RichViewerMock . type ) ;
expect ( wrapper . findComponent ( RichViewer ) . exists ( ) ) . toBe ( true ) ;
} ) ;
2020-03-13 15:44:24 +05:30
} ) ;
} ) ;
} ) ;
} ) ;
2020-05-24 23:13:21 +05:30
describe ( 'functionality' , ( ) => {
describe ( 'render error' , ( ) => {
2022-11-25 23:54:43 +05:30
const findContentEl = ( ) => wrapper . findComponent ( BlobContent ) ;
2020-05-24 23:13:21 +05:30
it ( 'correctly sets blob on the blob-content-error component' , ( ) => {
createComponent ( ) ;
expect ( findContentEl ( ) . props ( 'blob' ) ) . toEqual ( BlobMock ) ;
} ) ;
it ( ` refetches blob content on ${ BLOB _RENDER _EVENT _LOAD } event ` , ( ) => {
createComponent ( ) ;
expect ( wrapper . vm . $apollo . queries . blobContent . refetch ) . not . toHaveBeenCalled ( ) ;
findContentEl ( ) . vm . $emit ( BLOB _RENDER _EVENT _LOAD ) ;
expect ( wrapper . vm . $apollo . queries . blobContent . refetch ) . toHaveBeenCalledTimes ( 1 ) ;
} ) ;
it ( ` sets ' ${ SimpleViewerMock . type } ' as active on ${ BLOB _RENDER _EVENT _SHOW _SOURCE } event ` , ( ) => {
2020-07-28 23:09:34 +05:30
createComponent ( {
data : {
2020-05-24 23:13:21 +05:30
activeViewerType : RichViewerMock . type ,
} ,
2020-07-28 23:09:34 +05:30
} ) ;
2020-05-24 23:13:21 +05:30
findContentEl ( ) . vm . $emit ( BLOB _RENDER _EVENT _SHOW _SOURCE ) ;
expect ( wrapper . vm . activeViewerType ) . toEqual ( SimpleViewerMock . type ) ;
} ) ;
} ) ;
} ) ;
2020-03-13 15:44:24 +05:30
} ) ;