Skip to content

Commit

Permalink
Add IT for loading state
Browse files Browse the repository at this point in the history
  • Loading branch information
jepsar committed Dec 29, 2023
1 parent ef0d3ca commit 5e7fabb
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* The MIT License
*
* Copyright (c) 2009-2023 PrimeTek Informatics
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.primefaces.integrationtests.commandbutton;

import java.io.Serializable;

import javax.faces.view.ViewScoped;
import javax.inject.Named;

import org.primefaces.integrationtests.general.utilities.TestUtils;

import lombok.Data;

@Named
@ViewScoped
@Data
public class CommandButton001 implements Serializable {

private static final long serialVersionUID = 1L;

public void submit() {
TestUtils.addMessage("Submit");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:p="http://primefaces.org/ui">

<f:view contentType="text/html;charset=UTF-8" encoding="UTF-8">
<h:head/>

<h:body>
<h:form id="form">
<h:panelGroup>
Hello
</h:panelGroup>

<p:commandButton id="button" value="Submit" update="@previous"
action="#{commandButton001.submit()}"
disableOnAjax="true"/>
</h:form>
</h:body>
</f:view>

</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* The MIT License
*
* Copyright (c) 2009-2023 PrimeTek Informatics
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.primefaces.integrationtests.commandbutton;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.openqa.selenium.support.FindBy;
import org.primefaces.selenium.AbstractPrimePage;
import org.primefaces.selenium.AbstractPrimePageTest;
import org.primefaces.selenium.PrimeSelenium;
import org.primefaces.selenium.component.CommandButton;

public class CommandButton001Test extends AbstractPrimePageTest {

@Test
@Order(1)
@DisplayName("CommandButton: loading state")
void loadingState(Page page) {
// Arrange
CommandButton button = page.button;
setMinLoadAnim(100);

// Act
page.button.click();

// Assert
assertTrue(PrimeSelenium.hasCssClass(button, "ui-state-loading"));
}

@Test
@Order(2)
@DisplayName("CommandButton: loading state 2")
void loadingState2(Page page) {
// Arrange
CommandButton button = page.button;
noMinLoadAnim();

// Act
page.button.click();

// Assert
assertFalse(PrimeSelenium.hasCssClass(button, "ui-state-loading"));
}

public static class Page extends AbstractPrimePage {
@FindBy(id = "form:button")
CommandButton button;

@Override
public String getLocation() {
return "commandbutton/commandButton001.xhtml";
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,14 @@ protected void assertCss(WebElement element, String... cssClasses) {
}

protected void noMinLoadAnim() {
PrimeSelenium.executeScript("PrimeFaces.ajax.minLoadAnim = 0;");
setMinLoadAnim(0);
}

protected void setMinLoadAnim(int milliseconds) {
if (milliseconds < 0) {
throw new IllegalArgumentException("milliseconds cannot be negative");
}
PrimeSelenium.executeScript("PrimeFaces.ajax.minLoadAnim = " + milliseconds + ";");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@
}

button.addClass('ui-state-loading');
button.data('ajaxstart', Date.now());
widget.ajaxStart = Date.now();

if (typeof widget.disable === 'function'
&& widget.cfg.disableOnAjax !== false) {
Expand All @@ -424,7 +424,7 @@

PrimeFaces.queueTask(
function(){ PrimeFaces.buttonEndAjaxDisabled(widget, button); },
Math.max(PrimeFaces.ajax.minLoadAnim + button.data('ajaxstart') - Date.now(), 0)
Math.max(PrimeFaces.ajax.minLoadAnim + widget.ajaxStart - Date.now(), 0)
);
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* CommandLink is an extended version of standard commandLink with AJAX and theming.
*
* @prop {number} [ajaxCount] Number of concurrent active Ajax requests.
* @prop {number} [ajaxCount] Timestamp of the Ajax request that started the animation.
*
* @interface {PrimeFaces.widget.CommandLinkCfg} cfg The configuration for the {@link CommandLink| CommandLink widget}.
* You can access this configuration via {@link PrimeFaces.widget.BaseWidget.cfg|BaseWidget.cfg}. Please note that this
Expand Down Expand Up @@ -56,7 +57,7 @@ PrimeFaces.widget.CommandLink = PrimeFaces.widget.BaseWidget.extend({
}
if (PrimeFaces.ajax.Utils.isXhrSource($this, settings)) {
$this.jq.addClass('ui-state-loading');
$this.jq.data('ajaxstart', Date.now());
$this.ajaxStart = Date.now();
$this.disable();
}
}).on('pfAjaxComplete.' + this.id, function(e, xhr, settings, args) {
Expand All @@ -67,7 +68,7 @@ PrimeFaces.widget.CommandLink = PrimeFaces.widget.BaseWidget.extend({
if (PrimeFaces.ajax.Utils.isXhrSource($this, settings)) {
PrimeFaces.queueTask(
function(){ $this.endAjaxDisabled($this); },
Math.max(PrimeFaces.ajax.minLoadAnim + $this.jq.data('ajaxstart') - Date.now(), 0)
Math.max(PrimeFaces.ajax.minLoadAnim + $this.ajaxStart - Date.now(), 0)
);
}
});
Expand Down

0 comments on commit 5e7fabb

Please sign in to comment.