/
SelectCloudProjectModal.cy.tsx
183 lines (156 loc) · 7.06 KB
/
SelectCloudProjectModal.cy.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import { defaultMessages } from '@cy/i18n'
import {
CloudOrganizationConnectionStubs,
CloudUserStubs,
} from '@packages/graphql/test/stubCloudTypes'
import { SelectCloudProjectModalFragmentDoc } from '../../generated/graphql-test'
import SelectCloudProjectModal from '../modals/SelectCloudProjectModal.vue'
import { SelectCloudProjectModal_CreateCloudProjectDocument, SelectCloudProjectModal_SetProjectIdDocument } from '../../generated/graphql'
describe('<SelectCloudProjectModal />', () => {
function mountDialog (currentProjectName = 'Test Project') {
cy.mountFragment(SelectCloudProjectModalFragmentDoc, {
onResult: (result) => {
result.currentProject = {
__typename: 'CurrentProject' as const,
id: '1',
title: currentProjectName,
projectId: null,
}
result.cloudViewer = {
...CloudUserStubs.me,
organizations: CloudOrganizationConnectionStubs,
}
},
render (gql) {
return (<div class="h-screen">
<SelectCloudProjectModal utmMedium="test" gql={gql}/>
</div>)
},
})
}
it('selects the first organization by default', () => {
mountDialog()
// Implicitly asserts that organizations are sorted by name. CloudOrganizationConnectionStubs contains [org 2, org 1],
// while this ends up displayed as [org 1, org 2].
cy.get('[data-cy="selectOrganization"] button').should('contain.text', 'Test Org 1')
cy.get('[data-cy="external"]').should('have.attr', 'href').and('eq', 'https://on.cypress.io/adding-new-project')
cy.get('[data-cy="selectProject"] button').should('have.text', 'Pick a project').click()
// Asserting that projects should be sorted by name. CloudOrganizationConnectionStubs org 1 contains [project 2, project 1]
// but we assert that project 1 is first in the list.
cy.get('[role="listbox"] li').first().should('have.text', 'Test Project 1')
cy.contains('Test Project 1').click()
})
it('preselects project matching current name if one exists', () => {
mountDialog('Test Project 2')
cy.get('[data-cy="selectProject"] button').should('have.text', 'Test Project 2')
})
it('prefills new project name with the current one', () => {
mountDialog()
cy.contains('a', defaultMessages.runs.connect.modal.selectProject.createNewProject).click()
cy.findByLabelText(`${defaultMessages.runs.connect.modal.selectProject.projectName }*${defaultMessages.runs.connect.modal.selectProject.projectNameDisclaimer}`)
.should('have.value', 'Test Project')
cy.contains('a', defaultMessages.runs.connect.modal.selectProject.chooseExistingProject).should('be.visible')
cy.contains('label', defaultMessages.runs.connect.modal.selectProject.privateDescription).should('be.visible')
cy.contains('label', defaultMessages.runs.connect.modal.selectProject.publicDescription).should('be.visible')
})
it('can only choose an existing project if the organization has a project', () => {
mountDialog()
cy.get('[data-cy="selectOrganization"]').click()
cy.findByRole('listbox').within(() => cy.findAllByText('Test Org 2').click())
cy.contains('a', defaultMessages.runs.connect.modal.selectProject.chooseExistingProject).should('not.exist')
})
it('auto selects a project if it is the only project in the organization', () => {
mountDialog()
cy.get('[data-cy="selectOrganization"]').click()
cy.findByRole('listbox').within(() => cy.findAllByText('Test Org 3').click())
cy.get('[data-cy="selectProject"] button').should('have.text', 'Test Project 3')
})
it(`doesn't auto select a project if there are more than 1 projects in the org`, () => {
mountDialog()
cy.get('[data-cy="selectOrganization"]').click()
cy.findByRole('listbox').within(() => cy.findAllByText('Test Org 1').click())
cy.get('[data-cy="selectProject"] button').should('have.text', 'Pick a project')
})
it('shows the selected project when selecting from a list of >= 2 projects', () => {
mountDialog()
cy.get('[data-cy="selectOrganization"]').click()
cy.findByRole('listbox').within(() => cy.findAllByText('Test Org 1').click())
cy.get('[data-cy="selectProject"] button').click()
cy.contains('Test Project 2').click()
cy.get('[data-cy="selectProject"] button').should('have.text', 'Test Project 2').click()
cy.contains('Test Project 1').click()
cy.get('[data-cy="selectProject"] button').should('have.text', 'Test Project 1')
})
describe('create or connect project', () => {
beforeEach(() => {
const createMutation = cy.stub().as('createMutation')
const setProjectIdMutation = cy.stub().as('setProjectIdMutation')
cy.stubMutationResolver(SelectCloudProjectModal_CreateCloudProjectDocument, (defineResult, payload) => {
createMutation(payload)
return defineResult({
cloudProjectCreate: {
__typename: 'CloudProject',
id: '123',
slug: '123',
},
})
})
cy.stubMutationResolver(SelectCloudProjectModal_SetProjectIdDocument, (defineResult, payload) => {
setProjectIdMutation(payload)
return defineResult({
setProjectIdInConfigFile: {
__typename: 'Query',
currentProject: {
__typename: 'CurrentProject',
id: '123',
projectId: '123',
cloudProject: {
__typename: 'CloudProjectUnauthorized',
},
} as any,
},
})
})
mountDialog()
})
context('create new project', () => {
beforeEach(() => {
cy.contains('a', defaultMessages.runs.connect.modal.selectProject.createNewProject).click()
cy.contains('button', defaultMessages.runs.connect.modal.selectProject.createProject).click()
})
it('should call create project mutation with expected values', () => {
cy.get('@createMutation').should('have.been.calledOnceWith', {
campaign: 'Create project',
cohort: '',
medium: 'test',
name: 'Test Project',
orgId: '1',
public: false,
source: 'Binary: Launchpad',
})
})
it('should call setProjectId mutation', () => {
cy.get('@setProjectIdMutation').should('have.been.calledOnceWith', {
projectId: '123',
})
})
})
context('select existing project', () => {
beforeEach(() => {
cy.get('[data-cy="selectOrganization"]').click()
cy.findByRole('listbox').within(() => cy.findAllByText('Test Org 1').click())
cy.get('[data-cy="selectProject"] button').click()
cy.contains('Test Project 2').click()
cy.contains('button', defaultMessages.runs.connect.modal.selectProject.connectProject).click()
})
it('should not call create project mutation', () => {
cy.get('@createMutation').should('have.not.have.been.called')
})
it('should call setProjectId mutation', () => {
cy.get('@setProjectIdMutation').should('have.been.calledOnceWith', {
projectId: 'test-project-2',
})
})
})
})
})