Skip to content

Commit cc34778

Browse files
committed
test(ui-top-nav-bar): add tests
1 parent c2c8a4d commit cc34778

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,30 +21,38 @@
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
retries: {
33-
experimentalStrategy: 'detect-flake-and-pass-on-threshold',
33+
experimentalStrategy: "detect-flake-and-pass-on-threshold",
3434
experimentalOptions: {
35-
maxRetries: 10,
36-
passesRequired: 1
35+
maxRetries: 2,
36+
passesRequired: 1,
3737
},
3838
openMode: true,
39-
runMode: true
39+
runMode: true,
4040
},
41+
4142
screenshotOnRunFailure: false,
43+
4244
component: {
43-
excludeSpecPattern: 'regression-test/**',
45+
excludeSpecPattern: "regression-test/**",
4446
devServer: {
45-
framework: 'react',
46-
bundler: 'webpack',
47-
webpackConfig
48-
}
49-
}
50-
})
47+
framework: "react",
48+
bundler: "webpack",
49+
webpackConfig,
50+
},
51+
},
52+
53+
e2e: {
54+
setupNodeEvents(on, config) {
55+
// implement node event listeners here
56+
},
57+
},
58+
});

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)