2021-01-26 06:09:07 +00:00
import {
GlAlert ,
GlButton ,
GlDropdown ,
GlDropdownItem ,
GlFormSelect ,
GlLabel ,
GlSearchBoxByType ,
GlTable ,
} from '@gitlab/ui' ;
2020-07-15 15:09:21 +00:00
import { getByRole } from '@testing-library/dom' ;
2020-04-21 15:21:10 +00:00
import { mount , shallowMount } from '@vue/test-utils' ;
2020-07-15 15:09:21 +00:00
import AxiosMockAdapter from 'axios-mock-adapter' ;
2020-04-10 15:09:50 +00:00
import JiraImportForm from '~/jira_import/components/jira_import_form.vue' ;
2020-08-17 15:10:09 +00:00
import getJiraUserMappingMutation from '~/jira_import/queries/get_jira_user_mapping.mutation.graphql' ;
import initiateJiraImportMutation from '~/jira_import/queries/initiate_jira_import.mutation.graphql' ;
2021-01-26 06:09:07 +00:00
import searchProjectMembersQuery from '~/jira_import/queries/search_project_members.query.graphql' ;
2021-02-14 18:09:20 +00:00
import axios from '~/lib/utils/axios_utils' ;
2020-08-12 15:10:02 +00:00
import {
imports ,
issuesPath ,
jiraProjects ,
2020-08-27 15:10:21 +00:00
jiraUsersResponse ,
2020-08-17 15:10:09 +00:00
projectId ,
projectPath ,
2020-08-12 15:10:02 +00:00
userMappings as defaultUserMappings ,
} from '../mock_data' ;
2020-04-21 15:21:10 +00:00
2020-04-10 15:09:50 +00:00
describe ( 'JiraImportForm' , ( ) => {
2020-07-15 15:09:21 +00:00
let axiosMock ;
2020-08-17 15:10:09 +00:00
let mutateSpy ;
2021-01-26 06:09:07 +00:00
let querySpy ;
2020-04-10 15:09:50 +00:00
let wrapper ;
2020-07-15 15:09:21 +00:00
const currentUsername = 'mrgitlab' ;
2020-08-12 15:10:02 +00:00
const getAlert = ( ) => wrapper . find ( GlAlert ) ;
2020-07-15 15:09:21 +00:00
2020-04-30 15:09:46 +00:00
const getSelectDropdown = ( ) => wrapper . find ( GlFormSelect ) ;
2020-07-22 18:09:27 +00:00
const getContinueButton = ( ) => wrapper . find ( GlButton ) ;
2020-04-21 15:21:10 +00:00
const getCancelButton = ( ) => wrapper . findAll ( GlButton ) . at ( 1 ) ;
2020-04-10 15:09:50 +00:00
2020-08-12 15:10:02 +00:00
const getLabel = ( ) => wrapper . find ( GlLabel ) ;
2020-07-22 18:09:27 +00:00
const getTable = ( ) => wrapper . find ( GlTable ) ;
2020-09-14 12:09:34 +00:00
const getUserDropdown = ( ) => getTable ( ) . find ( GlDropdown ) ;
2020-07-22 18:09:27 +00:00
2020-12-23 21:10:24 +00:00
const getHeader = ( name ) => getByRole ( wrapper . element , 'columnheader' , { name } ) ;
2020-07-15 15:09:21 +00:00
2020-08-27 15:10:21 +00:00
const findLoadMoreUsersButton = ( ) => wrapper . find ( '[data-testid="load-more-users-button"]' ) ;
2020-07-22 18:09:27 +00:00
const mountComponent = ( {
2020-08-27 15:10:21 +00:00
hasMoreUsers = false ,
2020-07-22 18:09:27 +00:00
isSubmitting = false ,
2020-08-17 15:10:09 +00:00
loading = false ,
mutate = mutateSpy ,
2020-08-12 15:10:02 +00:00
selectedProject = 'MTG' ,
2020-07-22 18:09:27 +00:00
userMappings = defaultUserMappings ,
mountFunction = shallowMount ,
} = { } ) =>
2020-07-15 15:09:21 +00:00
mountFunction ( JiraImportForm , {
propsData : {
issuesPath ,
2020-08-12 15:10:02 +00:00
jiraImports : imports ,
2020-07-15 15:09:21 +00:00
jiraProjects ,
2020-08-17 15:10:09 +00:00
projectId ,
projectPath ,
2020-07-15 15:09:21 +00:00
} ,
data : ( ) => ( {
2020-08-27 15:10:21 +00:00
hasMoreUsers ,
2020-07-15 15:09:21 +00:00
isFetching : false ,
2020-08-17 15:10:09 +00:00
isSubmitting ,
2020-07-15 15:09:21 +00:00
searchTerm : '' ,
2020-08-12 15:10:02 +00:00
selectedProject ,
2020-07-15 15:09:21 +00:00
selectState : null ,
users : [ ] ,
2020-08-17 15:10:09 +00:00
userMappings ,
2020-07-15 15:09:21 +00:00
} ) ,
2020-08-17 15:10:09 +00:00
mocks : {
$apollo : {
loading ,
mutate ,
2021-01-26 06:09:07 +00:00
query : querySpy ,
2020-08-17 15:10:09 +00:00
} ,
} ,
2020-07-15 15:09:21 +00:00
currentUsername ,
} ) ;
beforeEach ( ( ) => {
axiosMock = new AxiosMockAdapter ( axios ) ;
2021-01-26 06:09:07 +00:00
mutateSpy = jest . fn ( ) . mockResolvedValue ( {
data : {
jiraImportStart : { errors : [ ] } ,
jiraImportUsers : { jiraUsers : [ ] , errors : [ ] } ,
} ,
} ) ;
querySpy = jest . fn ( ) . mockResolvedValue ( {
data : { project : { projectMembers : { nodes : [ ] } } } ,
} ) ;
2020-07-15 15:09:21 +00:00
} ) ;
2020-04-10 15:09:50 +00:00
afterEach ( ( ) => {
2020-07-15 15:09:21 +00:00
axiosMock . restore ( ) ;
2020-08-17 15:10:09 +00:00
mutateSpy . mockRestore ( ) ;
2021-01-26 06:09:07 +00:00
querySpy . mockRestore ( ) ;
2020-04-10 15:09:50 +00:00
wrapper . destroy ( ) ;
wrapper = null ;
} ) ;
2020-08-12 15:10:02 +00:00
describe ( 'select dropdown project selection' , ( ) => {
2020-04-21 15:21:10 +00:00
it ( 'is shown' , ( ) => {
wrapper = mountComponent ( ) ;
2020-04-10 15:09:50 +00:00
2020-08-28 12:10:37 +00:00
expect ( wrapper . find ( GlFormSelect ) . exists ( ) ) . toBe ( true ) ;
2020-04-21 15:21:10 +00:00
} ) ;
2020-04-10 15:09:50 +00:00
2020-04-21 15:21:10 +00:00
it ( 'contains a list of Jira projects to select from' , ( ) => {
2020-07-15 15:09:21 +00:00
wrapper = mountComponent ( { mountFunction : mount } ) ;
2020-04-21 15:21:10 +00:00
2020-04-30 15:09:46 +00:00
getSelectDropdown ( )
2020-04-21 15:21:10 +00:00
. findAll ( 'option' )
. wrappers . forEach ( ( optionEl , index ) => {
2020-07-15 15:09:21 +00:00
expect ( optionEl . text ( ) ) . toBe ( jiraProjects [ index ] . text ) ;
2020-04-21 15:21:10 +00:00
} ) ;
} ) ;
2020-07-15 15:09:21 +00:00
2020-08-12 15:10:02 +00:00
describe ( 'when selected project has been imported before' , ( ) => {
it ( 'shows jira-import::MTG-3 label since project MTG has been imported 2 time before' , ( ) => {
wrapper = mountComponent ( ) ;
expect ( getLabel ( ) . props ( 'title' ) ) . toBe ( 'jira-import::MTG-3' ) ;
} ) ;
it ( 'shows warning alert to explain project MTG has been imported 2 times before' , ( ) => {
wrapper = mountComponent ( { mountFunction : mount } ) ;
expect ( getAlert ( ) . text ( ) ) . toBe (
'You have imported from this project 2 times before. Each new import will create duplicate issues.' ,
) ;
} ) ;
} ) ;
2020-07-15 15:09:21 +00:00
2020-08-12 15:10:02 +00:00
describe ( 'when selected project has not been imported before' , ( ) => {
beforeEach ( ( ) => {
wrapper = mountComponent ( { selectedProject : 'MJP' } ) ;
} ) ;
it ( 'shows jira-import::MJP-1 label since project MJP has not been imported before' , ( ) => {
expect ( getLabel ( ) . props ( 'title' ) ) . toBe ( 'jira-import::MJP-1' ) ;
} ) ;
2020-07-15 15:09:21 +00:00
2020-08-12 15:10:02 +00:00
it ( 'does not show warning alert since project MJP has not been imported before' , ( ) => {
expect ( getAlert ( ) . exists ( ) ) . toBe ( false ) ;
} ) ;
2020-07-15 15:09:21 +00:00
} ) ;
2020-04-10 15:09:50 +00:00
} ) ;
2020-04-21 15:21:10 +00:00
describe ( 'form information' , ( ) => {
beforeEach ( ( ) => {
wrapper = mountComponent ( ) ;
} ) ;
2020-07-15 15:09:21 +00:00
it ( 'shows a heading for the user mapping section' , ( ) => {
expect (
getByRole ( wrapper . element , 'heading' , { name : 'Jira-GitLab user mapping template' } ) ,
) . toBeTruthy ( ) ;
} ) ;
2020-04-21 15:21:10 +00:00
it ( 'shows information to the user' , ( ) => {
expect ( wrapper . find ( 'p' ) . text ( ) ) . toBe (
2020-07-29 00:09:37 +00:00
'Jira users have been imported from the configured Jira instance. They can be mapped by selecting a GitLab user from the dropdown in the "GitLab username" column. When the form appears, the dropdown defaults to the user conducting the import.' ,
2020-04-21 15:21:10 +00:00
) ;
} ) ;
2020-07-15 15:09:21 +00:00
} ) ;
2020-04-21 15:21:10 +00:00
2020-07-15 15:09:21 +00:00
describe ( 'table' , ( ) => {
describe ( 'headers' , ( ) => {
beforeEach ( ( ) => {
wrapper = mountComponent ( { mountFunction : mount } ) ;
} ) ;
2020-04-21 15:21:10 +00:00
2020-07-15 15:09:21 +00:00
it ( 'has a "Jira display name" column' , ( ) => {
expect ( getHeader ( 'Jira display name' ) ) . toBeTruthy ( ) ;
} ) ;
2020-04-21 15:21:10 +00:00
2020-07-15 15:09:21 +00:00
it ( 'has an "arrow" column' , ( ) => {
expect ( getHeader ( 'Arrow' ) ) . toBeTruthy ( ) ;
} ) ;
2020-04-10 15:09:50 +00:00
2020-07-15 15:09:21 +00:00
it ( 'has a "GitLab username" column' , ( ) => {
expect ( getHeader ( 'GitLab username' ) ) . toBeTruthy ( ) ;
} ) ;
2020-04-21 15:21:10 +00:00
} ) ;
2020-07-15 15:09:21 +00:00
describe ( 'body' , ( ) => {
it ( 'shows all user mappings' , ( ) => {
wrapper = mountComponent ( { mountFunction : mount } ) ;
2020-07-22 18:09:27 +00:00
expect ( getTable ( ) . findAll ( 'tbody tr' ) ) . toHaveLength ( 2 ) ;
2020-07-15 15:09:21 +00:00
} ) ;
it ( 'shows correct information in each cell' , ( ) => {
wrapper = mountComponent ( { mountFunction : mount } ) ;
2020-07-22 18:09:27 +00:00
expect ( getTable ( ) . element ) . toMatchSnapshot ( ) ;
} ) ;
describe ( 'when there is no Jira->GitLab user mapping' , ( ) => {
it ( 'shows the logged in user in the dropdown' , ( ) => {
wrapper = mountComponent ( {
mountFunction : mount ,
userMappings : [
{
jiraAccountId : 'aei23f98f-q23fj98qfj' ,
jiraDisplayName : 'Jane Doe' ,
jiraEmail : 'janedoe@example.com' ,
gitlabId : undefined ,
gitlabUsername : undefined ,
} ,
] ,
} ) ;
expect ( getUserDropdown ( ) . text ( ) ) . toContain ( currentUsername ) ;
} ) ;
} ) ;
describe ( 'when there is a Jira->GitLab user mapping' , ( ) => {
it ( 'shows the mapped user in the dropdown' , ( ) => {
const gitlabUsername = 'mai' ;
wrapper = mountComponent ( {
mountFunction : mount ,
userMappings : [
{
jiraAccountId : 'aei23f98f-q23fj98qfj' ,
jiraDisplayName : 'Jane Doe' ,
jiraEmail : 'janedoe@example.com' ,
gitlabId : 14 ,
gitlabUsername ,
} ,
] ,
} ) ;
expect ( getUserDropdown ( ) . text ( ) ) . toContain ( gitlabUsername ) ;
} ) ;
2020-07-15 15:09:21 +00:00
} ) ;
2020-04-21 15:21:10 +00:00
} ) ;
2020-04-10 15:09:50 +00:00
} ) ;
2021-01-26 06:09:07 +00:00
describe ( 'member search' , ( ) => {
describe ( 'when searching for a member' , ( ) => {
beforeEach ( ( ) => {
querySpy = jest . fn ( ) . mockResolvedValue ( {
data : {
project : {
projectMembers : {
nodes : [
{
user : {
id : 7 ,
name : 'Frederic Chopin' ,
username : 'fchopin' ,
} ,
} ,
] ,
} ,
} ,
} ,
} ) ;
wrapper = mountComponent ( { mountFunction : mount } ) ;
wrapper . find ( GlSearchBoxByType ) . vm . $emit ( 'input' , 'fred' ) ;
} ) ;
it ( 'makes a GraphQL call' , ( ) => {
const queryArgument = {
query : searchProjectMembersQuery ,
variables : {
fullPath : projectPath ,
search : 'fred' ,
} ,
} ;
expect ( querySpy ) . toHaveBeenCalledWith ( expect . objectContaining ( queryArgument ) ) ;
} ) ;
it ( 'updates the user list' , ( ) => {
expect ( getUserDropdown ( ) . findAll ( GlDropdownItem ) ) . toHaveLength ( 1 ) ;
expect ( getUserDropdown ( ) . find ( GlDropdownItem ) . text ( ) ) . toContain (
'fchopin (Frederic Chopin)' ,
) ;
} ) ;
} ) ;
} ) ;
2020-07-15 15:09:21 +00:00
describe ( 'buttons' , ( ) => {
describe ( '"Continue" button' , ( ) => {
it ( 'is shown' , ( ) => {
wrapper = mountComponent ( ) ;
2020-04-21 15:21:10 +00:00
2020-07-22 18:09:27 +00:00
expect ( getContinueButton ( ) . text ( ) ) . toBe ( 'Continue' ) ;
2020-07-15 15:09:21 +00:00
} ) ;
it ( 'is in loading state when the form is submitting' , async ( ) => {
wrapper = mountComponent ( { isSubmitting : true } ) ;
2020-04-10 15:09:50 +00:00
2020-07-22 18:09:27 +00:00
expect ( getContinueButton ( ) . props ( 'loading' ) ) . toBe ( true ) ;
2020-07-15 15:09:21 +00:00
} ) ;
2020-04-21 15:21:10 +00:00
} ) ;
2020-04-10 15:09:50 +00:00
2020-07-15 15:09:21 +00:00
describe ( '"Cancel" button' , ( ) => {
beforeEach ( ( ) => {
wrapper = mountComponent ( ) ;
} ) ;
2020-04-30 15:09:46 +00:00
2020-07-15 15:09:21 +00:00
it ( 'is shown' , ( ) => {
expect ( getCancelButton ( ) . text ( ) ) . toBe ( 'Cancel' ) ;
} ) ;
2020-04-21 15:21:10 +00:00
2020-07-15 15:09:21 +00:00
it ( 'links to the Issues page' , ( ) => {
expect ( getCancelButton ( ) . attributes ( 'href' ) ) . toBe ( issuesPath ) ;
} ) ;
} ) ;
2020-04-30 15:09:46 +00:00
} ) ;
2020-08-17 15:10:09 +00:00
describe ( 'submitting the form' , ( ) => {
it ( 'initiates the Jira import mutation with the expected arguments' , ( ) => {
wrapper = mountComponent ( ) ;
2020-08-12 15:10:02 +00:00
2020-08-17 15:10:09 +00:00
const mutationArguments = {
mutation : initiateJiraImportMutation ,
variables : {
input : {
jiraProjectKey : 'MTG' ,
projectPath ,
usersMapping : [
{
jiraAccountId : 'aei23f98f-q23fj98qfj' ,
gitlabId : 15 ,
} ,
{
jiraAccountId : 'fu39y8t34w-rq3u289t3h4i' ,
gitlabId : undefined ,
} ,
] ,
} ,
} ,
} ;
2020-04-21 15:21:10 +00:00
2020-07-15 15:09:21 +00:00
wrapper . find ( 'form' ) . trigger ( 'submit' ) ;
2020-04-10 15:09:50 +00:00
2020-08-17 15:10:09 +00:00
expect ( mutateSpy ) . toHaveBeenCalledWith ( expect . objectContaining ( mutationArguments ) ) ;
} ) ;
} ) ;
describe ( 'on mount GraphQL user mapping mutation' , ( ) => {
it ( 'is called with the expected arguments' , ( ) => {
wrapper = mountComponent ( ) ;
const mutationArguments = {
mutation : getJiraUserMappingMutation ,
variables : {
input : {
projectPath ,
2020-08-27 15:10:21 +00:00
startAt : 0 ,
2020-08-17 15:10:09 +00:00
} ,
} ,
} ;
expect ( mutateSpy ) . toHaveBeenCalledWith ( expect . objectContaining ( mutationArguments ) ) ;
} ) ;
describe ( 'when there is an error when called' , ( ) => {
beforeEach ( ( ) => {
const mutate = jest . fn ( ( ) => Promise . reject ( ) ) ;
wrapper = mountComponent ( { mutate } ) ;
} ) ;
it ( 'shows error message' , ( ) => {
expect ( getAlert ( ) . exists ( ) ) . toBe ( true ) ;
} ) ;
2020-07-15 15:09:21 +00:00
} ) ;
2020-04-10 15:09:50 +00:00
} ) ;
2020-08-27 15:10:21 +00:00
describe ( 'load more users button' , ( ) => {
describe ( 'when all users have been loaded' , ( ) => {
it ( 'is not shown' , ( ) => {
wrapper = mountComponent ( ) ;
expect ( findLoadMoreUsersButton ( ) . exists ( ) ) . toBe ( false ) ;
} ) ;
} ) ;
describe ( 'when all users have not been loaded' , ( ) => {
it ( 'is shown' , ( ) => {
wrapper = mountComponent ( { hasMoreUsers : true } ) ;
expect ( findLoadMoreUsersButton ( ) . exists ( ) ) . toBe ( true ) ;
} ) ;
} ) ;
describe ( 'when clicked' , ( ) => {
beforeEach ( ( ) => {
mutateSpy = jest . fn ( ( ) =>
Promise . resolve ( {
data : {
jiraImportStart : { errors : [ ] } ,
jiraImportUsers : { jiraUsers : jiraUsersResponse , errors : [ ] } ,
} ,
} ) ,
) ;
wrapper = mountComponent ( { hasMoreUsers : true } ) ;
} ) ;
it ( 'calls the GraphQL user mapping mutation' , async ( ) => {
const mutationArguments = {
mutation : getJiraUserMappingMutation ,
variables : {
input : {
projectPath ,
startAt : 0 ,
} ,
} ,
} ;
findLoadMoreUsersButton ( ) . vm . $emit ( 'click' ) ;
expect ( mutateSpy ) . toHaveBeenCalledWith ( expect . objectContaining ( mutationArguments ) ) ;
} ) ;
} ) ;
} ) ;
2020-04-10 15:09:50 +00:00
} ) ;