-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
200 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { mount } from "enzyme"; | ||
import React from "react"; | ||
import configureStore from "redux-mock-store"; | ||
import { Provider } from "react-redux"; | ||
import { MemoryRouter } from "react-router-dom"; | ||
import { checkAuth } from "../../utils/checkAuth"; | ||
import { EditArticle } from "../../views/EditArticle"; | ||
import { article } from "../__mocks__/testData"; | ||
|
||
describe.only("should test the check authenticated component", () => { | ||
const initialState = { article }; | ||
const mockStore = configureStore(); | ||
let wrapper; | ||
let store; | ||
let ConditionalComponent; | ||
const nextUrl = "/articles/new"; | ||
beforeEach(() => { | ||
const props = { | ||
article, | ||
match: { | ||
params: { | ||
slug: "hello-world" | ||
} | ||
}, | ||
response: "", | ||
location: { pathname: nextUrl }, | ||
history: [] | ||
}; | ||
store = mockStore(initialState); | ||
ConditionalComponent = checkAuth(EditArticle); | ||
wrapper = mount( | ||
<Provider store={store}> | ||
<MemoryRouter> | ||
<ConditionalComponent {...props} /> | ||
</MemoryRouter> | ||
</Provider> | ||
).find("AuthenticatedComponent"); | ||
}); | ||
afterEach(() => {}); | ||
test("should redirect to next url if not logged in", () => { | ||
expect(wrapper.props().history).toContain(`/sign_in?next=${nextUrl}`); | ||
}); | ||
|
||
test("should return next component if the user is authenticated", () => { | ||
// this test is giving me headaches | ||
const localStorageProto = Object.getPrototypeOf(localStorage); | ||
jest | ||
.spyOn(localStorageProto, "getItem") | ||
.mockImplementation(() => "fake- from test token"); | ||
expect(wrapper.isEmptyRender()).not.toBeTruthy(); | ||
localStorageProto.getItem.mockRestore(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import React from "react"; | ||
import { connect } from "react-redux"; | ||
import PropTypes from "prop-types"; | ||
|
||
const checkAuth = Component => { | ||
class AuthenticatedComponent extends React.Component { | ||
componentWillMount() { | ||
this.checkAuth(); | ||
} | ||
|
||
async checkAuth() { | ||
const { location, history } = this.props; | ||
const token = await localStorage.getItem("token"); | ||
const currentUrl = location.pathname; | ||
if (!token) { | ||
history.push(`/sign_in?next=${currentUrl}`); | ||
} | ||
} | ||
|
||
render() { | ||
const token = localStorage.getItem("token"); | ||
return <div>{token ? <Component {...this.props} /> : ""}</div>; | ||
} | ||
} | ||
|
||
AuthenticatedComponent.propTypes = { | ||
location: PropTypes.shape({ | ||
pathname: PropTypes.string.isRequired | ||
}).isRequired, | ||
history: PropTypes.any.isRequired | ||
}; | ||
return connect()(AuthenticatedComponent); | ||
}; | ||
export { checkAuth }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters