2020-07-28 08:09:49 -04:00
import { nextTick } from 'vue' ;
2020-02-14 13:08:45 -05:00
import { mount } from '@vue/test-utils' ;
2020-08-17 17:09:56 -04:00
import {
Blob as BlobMock ,
SimpleViewerMock ,
RichViewerMock ,
RichBlobContentMock ,
SimpleBlobContentMock ,
} from 'jest/blob/components/mock_data' ;
2020-01-31 10:08:42 -05:00
import SnippetBlobView from '~/snippets/components/snippet_blob_view.vue' ;
2020-02-11 07:08:52 -05:00
import BlobHeader from '~/blob/components/blob_header.vue' ;
2020-02-14 13:08:45 -05:00
import BlobContent from '~/blob/components/blob_content.vue' ;
2020-05-26 08:08:22 -04:00
import {
BLOB _RENDER _EVENT _LOAD ,
BLOB _RENDER _EVENT _SHOW _SOURCE ,
BLOB _RENDER _ERRORS ,
} from '~/blob/components/constants' ;
2020-02-14 13:08:45 -05:00
import { RichViewer , SimpleViewer } from '~/vue_shared/components/blob_viewers' ;
2020-07-24 08:09:34 -04:00
import { SNIPPET _VISIBILITY _PUBLIC } from '~/snippets/constants' ;
2020-01-31 10:08:42 -05:00
describe ( 'Blob Embeddable' , ( ) => {
let wrapper ;
const snippet = {
id : 'gid://foo.bar/snippet' ,
webUrl : 'https://foo.bar' ,
visibilityLevel : SNIPPET _VISIBILITY _PUBLIC ,
} ;
2020-02-14 13:08:45 -05:00
const dataMock = {
activeViewerType : SimpleViewerMock . type ,
} ;
2020-01-31 10:08:42 -05:00
2020-06-30 20:09:02 -04:00
function createComponent ( {
snippetProps = { } ,
data = dataMock ,
blob = BlobMock ,
contentLoading = false ,
} = { } ) {
2020-02-11 07:08:52 -05:00
const $apollo = {
queries : {
2020-02-14 13:08:45 -05:00
blobContent : {
loading : contentLoading ,
2020-05-18 08:08:08 -04:00
refetch : jest . fn ( ) ,
skip : true ,
2020-02-11 07:08:52 -05:00
} ,
} ,
} ;
2020-02-14 13:08:45 -05:00
wrapper = mount ( SnippetBlobView , {
2020-01-31 10:08:42 -05:00
propsData : {
snippet : {
... snippet ,
2020-06-30 20:09:02 -04:00
... snippetProps ,
2020-01-31 10:08:42 -05:00
} ,
2020-06-30 20:09:02 -04:00
blob ,
2020-01-31 10:08:42 -05:00
} ,
2020-02-14 13:08:45 -05:00
data ( ) {
return {
... data ,
} ;
} ,
2020-02-11 07:08:52 -05:00
mocks : { $apollo } ,
2020-01-31 10:08:42 -05:00
} ) ;
}
afterEach ( ( ) => {
wrapper . destroy ( ) ;
} ) ;
2020-02-11 07:08:52 -05:00
describe ( 'rendering' , ( ) => {
it ( 'renders correct components' , ( ) => {
createComponent ( ) ;
expect ( wrapper . find ( BlobHeader ) . exists ( ) ) . toBe ( true ) ;
2020-02-14 13:08:45 -05:00
expect ( wrapper . find ( BlobContent ) . exists ( ) ) . toBe ( true ) ;
2020-01-31 10:08:42 -05:00
} ) ;
2020-02-14 13:08:45 -05:00
it ( 'sets simple viewer correctly' , ( ) => {
createComponent ( ) ;
expect ( wrapper . find ( SimpleViewer ) . exists ( ) ) . toBe ( true ) ;
} ) ;
it ( 'sets rich viewer correctly' , ( ) => {
2020-05-07 17:09:26 -04:00
const data = { ... dataMock , activeViewerType : RichViewerMock . type } ;
2020-06-30 20:09:02 -04:00
createComponent ( {
data ,
} ) ;
2020-02-14 13:08:45 -05:00
expect ( wrapper . find ( RichViewer ) . exists ( ) ) . toBe ( true ) ;
} ) ;
it ( 'correctly switches viewer type' , ( ) => {
createComponent ( ) ;
expect ( wrapper . find ( SimpleViewer ) . exists ( ) ) . toBe ( true ) ;
wrapper . vm . switchViewer ( RichViewerMock . type ) ;
return wrapper . vm
. $nextTick ( )
. then ( ( ) => {
expect ( wrapper . find ( RichViewer ) . exists ( ) ) . toBe ( true ) ;
wrapper . vm . switchViewer ( SimpleViewerMock . type ) ;
} )
. then ( ( ) => {
expect ( wrapper . find ( SimpleViewer ) . exists ( ) ) . toBe ( true ) ;
} ) ;
} ) ;
2020-05-26 08:08:22 -04:00
it ( 'passes information about render error down to blob header' , ( ) => {
createComponent ( {
blob : {
... BlobMock ,
simpleViewer : {
... SimpleViewerMock ,
renderError : BLOB _RENDER _ERRORS . REASONS . COLLAPSED . id ,
} ,
} ,
} ) ;
expect ( wrapper . find ( BlobHeader ) . props ( 'hasRenderError' ) ) . toBe ( true ) ;
} ) ;
2020-07-28 08:09:49 -04:00
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 : {
2020-09-24 08:09:37 -04:00
nodes : [
2020-07-28 08:09:49 -04:00
{
2020-09-24 08:09:37 -04:00
blobs : {
nodes : snippetBlobs ,
2020-07-28 08:09:49 -04:00
} ,
} ,
] ,
} ,
} ;
createComponent ( {
blob : {
... BlobMock ,
path : currentBlob . path ,
} ,
} ) ;
// mimic apollo's update
wrapper . setData ( {
blobContent : wrapper . vm . onContentUpdate ( apolloData ) ,
} ) ;
await nextTick ( ) ;
const findContent = ( ) => wrapper . find ( BlobContent ) ;
expect ( findContent ( ) . props ( 'content' ) ) . toBe ( expectedContent ) ;
} ,
) ;
} ) ;
2020-02-14 13:08:45 -05:00
describe ( 'URLS with hash' , ( ) => {
beforeEach ( ( ) => {
window . location . hash = '#LC2' ;
} ) ;
afterEach ( ( ) => {
window . location . hash = '' ;
} ) ;
it ( 'renders simple viewer by default if URL contains hash' , ( ) => {
2020-06-30 20:09:02 -04:00
createComponent ( {
data : { } ,
} ) ;
2020-02-14 13:08:45 -05:00
expect ( wrapper . vm . activeViewerType ) . toBe ( SimpleViewerMock . type ) ;
expect ( wrapper . find ( SimpleViewer ) . exists ( ) ) . toBe ( true ) ;
} ) ;
describe ( 'switchViewer()' , ( ) => {
2020-04-14 05:09:34 -04:00
it ( 'switches to the passed viewer' , ( ) => {
2020-02-14 13:08:45 -05:00
createComponent ( ) ;
wrapper . vm . switchViewer ( RichViewerMock . type ) ;
return wrapper . vm
. $nextTick ( )
. then ( ( ) => {
expect ( wrapper . vm . activeViewerType ) . toBe ( RichViewerMock . type ) ;
expect ( wrapper . find ( RichViewer ) . exists ( ) ) . toBe ( true ) ;
wrapper . vm . switchViewer ( SimpleViewerMock . type ) ;
} )
. then ( ( ) => {
expect ( wrapper . vm . activeViewerType ) . toBe ( SimpleViewerMock . type ) ;
expect ( wrapper . find ( SimpleViewer ) . exists ( ) ) . toBe ( true ) ;
} ) ;
} ) ;
} ) ;
} ) ;
2020-01-31 10:08:42 -05:00
} ) ;
2020-05-18 08:08:08 -04:00
describe ( 'functionality' , ( ) => {
describe ( 'render error' , ( ) => {
const findContentEl = ( ) => wrapper . find ( BlobContent ) ;
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-06-30 20:09:02 -04:00
createComponent ( {
data : {
2020-05-18 08:08:08 -04:00
activeViewerType : RichViewerMock . type ,
} ,
2020-06-30 20:09:02 -04:00
} ) ;
2020-05-18 08:08:08 -04:00
findContentEl ( ) . vm . $emit ( BLOB _RENDER _EVENT _SHOW _SOURCE ) ;
expect ( wrapper . vm . activeViewerType ) . toEqual ( SimpleViewerMock . type ) ;
} ) ;
} ) ;
} ) ;
2020-01-31 10:08:42 -05:00
} ) ;