How to set NGRX state before a cypress e2e test? - ngrx

I am trying to write e2e tests for one of my UI pages which depend on NGRX state.
Not sure how to set the NGRX state before starting the test.
Could you please help me with this?

Have you read this article? https://www.cypress.io/blog/2018/11/14/testing-redux-store/
It details how to set up your tests to access / read state.
I would recommend following the steps there. For setting up the state of your store, you have two options:
Manually in the code dispatch the correct actions that will get your state into the position you need for the test. This could be done programatically, or via cypress itself by firing off click/select/type events.
Using NgRx 8, you can mock the store and mock selectors.
In a unit testing scenario I would argue using #1's programatic approach is a poor choice, but given this is an e2e test, both options are viable IMO.

Related

Redux Toolkit, Testing createAsyncThunk actions with it's cycles

I am trying to test createAsyncThunk actions the same way I test createAction but it fails and nothing changes in the reducers.
I have made a small app to simulate the cases I made createAsyncThunk and made some tests for it but unfortunately, any tests related to createAsyncThunk fail.
here's the Repo link ReduxToolkitTesting
Lib used this is Repo
Redux Toolkit (For Sure :D)
React-navigation (Assuming this is the most used navigation lib)
UI Kitten (Theming and Components ready to use)
Store consists of 2 reducers
Gallery:
Set loading indicator to true till fetch is done
Fetch random pic from public API and set loading indicator to false
If an error happened it reject with an error message and set loading indicator to false
Reset Picture to default state
Theme
Toggle to switch between dark or light theme (also createAsyncThunk cuz I need access to getState), I know I can just pass the value but I need it to be like this just for testing experiment.
I made tests for both reducers and for the Home page which contain buttons to simulate all these behaviors too,
also made render function as mentioned in Redux Doc
but and just add the store in the return from the render function with Jest.spyOn on all the functions in case of want to check specific action calls.
right now any test related to createAsyncThunk is failing and can't find any way to fix it or to test some functionalities in another way.
PS. This is might be a lot to ask but I won't remove this Repo and it would be huge if we could contribute to it to be something like a live example on how to test Redux Toolkit and add more scenarios with its a test for advanced testing would be awesome too.
Thanks in advance.

Redux - dispatch actions which happen frequently and bypass the store ?

I'm building a synthesizer which has a piano-style keyboard UI input.
The keyboard note on/off events can happen quite frequently, these are used to update different parts of the UI and to trigger audio.
What is the frequency threshold which Redux can handle events? For example, if an event occurs 60 times per second which needs to update some aspect of the UI, how would one handle that using Redux patterns ?
I'm fine with doing this event-ing outside of Redux store entirely if Redux doesn't handle this use case.
Redux doesn't have any magic built into it, it's just an immutable state handler, so whatever javascript can do, redux can do.
What you want to take care of are dom operations, and in your case I'm assuming sound operations.
so your optimizations would be more on the react side, not redux.
if you want help with that, share your relevant react code here.
In general, the place to start optimizing react is shouldComponentUpdate
EDIT:
Here are some links I've found that might give some guidance / inspiration:
https://github.com/xjamundx/react-piano
https://github.com/DanielDeychakiwsky/react-piano

How to access methods in forms from a test method in a different solution file?

I have two different projects in two separate solution files. One is a test project containing my unit test methods and other is the main project. I know I need to create an instance of the individual GUI form first in order to access the methods in but not sure how to do this.
Also, I have certain variable values which get initiated only when I run the entire original application. These values are used in many of the methods in the main project. Without them I will just get null values and the test method would just fail. Is there any way to get values for those variables without running the application? I tried to place the logic which fetches the value for those variables in my test method and then call the actual method but this still doesn't work. How do I resolve this problem?
The problems you have run into are quite common for trying to write unit tests for an application that is not well written with regards for testability.
Usually the code you write test for, does not reside in forms or UI classes.
The business logic should be located in separated classes within your main project. The business logic can then be called from your UI and from your unit test too.
So you have to move any business logic to separate classes first.
The next thing you need to do is remove any dependencies from the newly created classes to other classes which prevent you from writing a unit test and replace those dependencies with interfaces.
For instance you can change your application so that every environment variable which you mention in your question, is retrieved by special classes.
If you create interfaces for this classes you can setup a so called Mock ( a fake version of your real class) in you unit test which you can configure in the test to behave as desired for the particular test scenario.
Some general advice:
Often refactoring the code to be tested (i.e. making it better testable) takes more time than writing the unit test itself. There are whole books about refactoring so called "legacy" code.
Refactoring an application for testability often means to de-couple dependencies between concrete classes and to use interfaces instead. In production code you pass your production class for the interface, in a unit test you can create a "mock" class which implements the same interface but behaves in a way like needed in your test
When writing unit tests, you might consider using a mocking framework like Moq. It will save you a lot of time and make the test code smaller. You will find an introduction here: Unit Testing .NET Application with Moq Framework
Ideally you should design you application from the beginning in a way which has testability in mind. The "TDD" (test driven development) approach even goes one step further, here you write your tests before you write the actual code, but this approach is not used very often in my experience.

Tridion - Placing Item in Workflow from Event

Is it possible to place an item in workflow from the event system? The problem I am facing is that we would like to direct components to two different workflows based on what folder they are in (instead of what schema they use), which the Tridion UI doesn't seem to support. I was hoping to write an event that is triggered on check-in of a component, so I can then determine which folder that component is in and direct it into the appropriate workflow from the event.
I'm fine with creation of the event, I'm just totally lost on where to start as far as adding the item to workflow goes. I have looked at the TOM.NET API documentation but really haven't found anything that helps. Any assistance or examples would be appreciated.
As #Jeremy suggests, what you are actually trying to do is not possible.
However you can achieve the same outcome by making the second step of your workflow an automated decision which creates 2 separate workflow branches within one workflow process definition. Your automated decision can read the Org Item of the component and direct it accordingly.
This is not possible - a Component is only added to a WF process when it is saved and its Schema has an associated process definition.

Test WF 4.0 Delay activity -- Replace DurableTimerExtension with my own MockTimerExtension

I want to provide my own implementation of TimerExtention in place of DurableTimerExtention provided by Delay activity, so that I can test workflows that use delays.
How can I accomplish that?
I have WorkflowService instance and its root Activity. The workflow service is hosted in WorkflowServiceHost, (though I can not figure out how to get WorkflowInstance instance)
One way to test your workflows is to use the Microsoft.Activities.UnitTesting on CodePlex. It has the capability to mock activities using XAML Injection. See here for more details.
In the case of a Delay activity you can do things a lot simpler though. Instead of hard coding the Duration to a fixed value I normally create a config object with the duration in there. In the Delay activity I just enter an expression like Settings.WaitForPaymentDuration. In production the Settings.WaitForPaymentDuration will be set to something like 30 days but in a test I can set it to 1 second instead without changing the workflow at all.
I have had a similar interest in mocking the TimerExtension, I wrote a blog post about it. In that post I inject my own implementation of the TimerExtension using WorkflowApplication.Extensions.Add<T>(Func<T>). WorkflowServiceHost also has this collection. You could replace the concrete class I have in my example with a mock. I wrote it targeting 4.5, but it should work with anything that uses the Extension collection. Its in 4, not sure earlier versions.
[Link]

Resources