Skip to content

Commit b236c78

Browse files
git-nandorjoyenjoyer
authored andcommitted
test(ui-top-nav-bar): add tests
1 parent 01b66ab commit b236c78

File tree

5 files changed

+656
-13
lines changed

5 files changed

+656
-13
lines changed

cypress.config.ts

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@
2121
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2222
* SOFTWARE.
2323
*/
24-
import { defineConfig } from 'cypress'
24+
import { defineConfig } from "cypress";
2525

2626
// TODO figure out why this isn't working:
2727
// import webpackConfig from '@instructure/ui-webpack-config'
2828
// eslint-disable-next-line @instructure/no-relative-imports
29-
import webpackConfig from './packages/ui-karma-config/lib/legacyBaseWebpackConfig'
29+
import webpackConfig from "./packages/ui-karma-config/lib/legacyBaseWebpackConfig";
3030

3131
export default defineConfig({
3232
numTestsKeptInMemory: 1,
@@ -37,21 +37,29 @@ export default defineConfig({
3737
execTimeout: 120000,
3838
taskTimeout: 60000,
3939
retries: {
40-
experimentalStrategy: 'detect-flake-and-pass-on-threshold',
40+
experimentalStrategy: "detect-flake-and-pass-on-threshold",
4141
experimentalOptions: {
42-
maxRetries: 10,
43-
passesRequired: 1
42+
maxRetries: 2,
43+
passesRequired: 1,
4444
},
4545
openMode: true,
46-
runMode: true
46+
runMode: true,
4747
},
48+
4849
screenshotOnRunFailure: false,
50+
4951
component: {
50-
excludeSpecPattern: 'regression-test/**',
52+
excludeSpecPattern: "regression-test/**",
5153
devServer: {
52-
framework: 'react',
53-
bundler: 'webpack',
54-
webpackConfig
55-
}
56-
}
57-
})
54+
framework: "react",
55+
bundler: "webpack",
56+
webpackConfig,
57+
},
58+
},
59+
60+
e2e: {
61+
setupNodeEvents(on, config) {
62+
// implement node event listeners here
63+
},
64+
},
65+
});

cypress/component/SubNav.cy.tsx

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2015 - present Instructure, Inc.
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
import React from 'react'
25+
import { SubNav } from '../../packages/ui-top-nav-bar'
26+
import '../support/component'
27+
import 'cypress-real-events'
28+
29+
30+
describe('<SubNav/>', () => {
31+
it('should call onClick and redirect when item is clicked', async () => {
32+
const onClickSpy = cy.spy().as('onClickSpy')
33+
34+
cy.mount(
35+
<SubNav
36+
menuItems={[
37+
{ title: 'Home', href: '#', selected: true },
38+
{ title: 'Announcements', href: '#', selected: false },
39+
{ title: 'Assignments', href: '#-test', selected: false, onClick: onClickSpy }
40+
]}
41+
/>
42+
)
43+
cy.contains('Assignments').realClick()
44+
45+
cy.get('@onClickSpy').should('have.been.called')
46+
cy.url().should('contain', '#-test')
47+
})
48+
49+
it('should be able to select with keyboard', async () => {
50+
const onClickSpy = cy.spy().as('onClickSpy')
51+
cy.mount(
52+
<SubNav
53+
menuItems={[
54+
{ title: 'Home', href: '#'},
55+
{ title: 'Announcements', href: '#-test2', selected: false, onClick: onClickSpy },
56+
{ title: 'Assignments', href: '#-test', selected: true }
57+
]}
58+
/>
59+
)
60+
cy.contains('Home').focus()
61+
cy.realPress(['Tab']).wait(100)
62+
cy.realPress(['Enter']).wait(100)
63+
64+
cy.get('@onClickSpy').should('have.been.called')
65+
cy.url().should('contain', '#-test2')
66+
})
67+
})

0 commit comments

Comments
 (0)