-
-
Notifications
You must be signed in to change notification settings - Fork 43
Fix Anchor Link triggers transition #361
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,10 @@ | ||
const isNavigatingToAnotherPage = function(currentLocation, popstateEvent) { | ||
const targetLocation = popstateEvent.target.location; | ||
|
||
// omits hash by design | ||
return currentLocation.pathname !== targetLocation.pathname || | ||
currentLocation.origin !== targetLocation.origin || | ||
currentLocation.search !== targetLocation.search | ||
} | ||
|
||
export default isNavigatingToAnotherPage |
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -179,4 +179,140 @@ def response | |
expect(stripped(static_output)).to ( include(stripped(expected_static_output)) ) | ||
end | ||
|
||
it "behaves correctly with anchor links (no reload, retain anchor)" do | ||
class ExamplePage < Matestack::Ui::Page | ||
def response | ||
components { | ||
link path: "#someanchor", text: "go to anchor", id: "my-link" | ||
|
||
br times: 200 | ||
|
||
div id: "someanchor" do | ||
plain "hello!" | ||
end | ||
|
||
div id: "my-div" do | ||
plain "#{DateTime.now.strftime('%Q')}" | ||
end | ||
} | ||
end | ||
end | ||
|
||
visit "/example" | ||
|
||
element = page.find("#my-div") | ||
before_content = element.text | ||
|
||
# don't you rerender on me! | ||
expect(ExamplePage).not_to receive(:new) | ||
|
||
page.click_link("my-link") | ||
|
||
# if the page reloaded we'd have different content here but as we don't want reloads | ||
# we want the same | ||
expect(page).to have_css("#my-div", text: before_content) | ||
expect(page.current_url).to end_with("#someanchor") | ||
end | ||
|
||
describe "with an App" do | ||
|
||
after :each do | ||
class ExamplePage < Matestack::Ui::Page | ||
# as the test suites work with redefining classes and this | ||
# hack here was used to set a specific app easily, | ||
# we need to "restore" previous state | ||
def set_app_class | ||
super | ||
end | ||
end | ||
end | ||
|
||
it "behaves correctly with anchor links (no reload, retain anchor) even inside an app" do | ||
class Apps::MyTestApp < Matestack::Ui::App | ||
def response | ||
components { | ||
page_content | ||
} | ||
end | ||
end | ||
|
||
class ExamplePage < Matestack::Ui::Page | ||
def response | ||
components { | ||
link path: "#someanchor", text: "go to anchor", id: "my-link" | ||
|
||
br times: 200 | ||
|
||
div id: "someanchor" do | ||
plain "hello!" | ||
end | ||
|
||
div id: "my-div" do | ||
plain "#{DateTime.now.strftime('%Q')}" | ||
end | ||
} | ||
end | ||
|
||
# Hacky/instable but easy way to set my custom App for this page | ||
def set_app_class | ||
@app_class = Apps::MyTestApp | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as the comment states, this is hacky but considering doing naming convetions, a new controller and appending to the routes etc. this seemed preferable to me. |
||
end | ||
end | ||
|
||
visit "/example" | ||
|
||
element = page.find("#my-div") | ||
before_content = element.text | ||
|
||
# don't you rerender on me! | ||
expect(ExamplePage).not_to receive(:new) | ||
|
||
page.click_link("my-link") | ||
|
||
# if the page reloaded we'd have different content here but as we don't want reloads | ||
# we want the same | ||
expect(page).to have_css("#my-div", text: before_content) | ||
expect(page.current_url).to end_with("#someanchor") | ||
end | ||
|
||
it "just changing the search string will still reload the page" do | ||
class Apps::MyTestApp < Matestack::Ui::App | ||
def response | ||
components { | ||
page_content | ||
} | ||
end | ||
end | ||
|
||
class ExamplePage < Matestack::Ui::Page | ||
def response | ||
components { | ||
link path: "?a=true", text: "go to anchor", id: "my-link" | ||
|
||
br times: 200 | ||
|
||
div id: "my-div" do | ||
plain "#{DateTime.now.strftime('%Q')}" | ||
end | ||
} | ||
end | ||
|
||
# Hacky/instable but easy way to set my custom App for this page | ||
def set_app_class | ||
@app_class = Apps::MyTestApp | ||
end | ||
end | ||
|
||
visit "/example" | ||
|
||
element = page.find("#my-div") | ||
before_content = element.text | ||
|
||
page.click_link("my-link") | ||
|
||
expect(page).to have_css("#my-div") | ||
expect(page).to have_no_css("#my-div", text: before_content) | ||
expect(page.current_url).to end_with("?a=true") | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wasn't used anywhere, in the file so I removed it. I hope I'm not unaware of some side effect this had, but the tests pass 🤷♂️