Add project files.
This commit is contained in:
+35
@@ -0,0 +1,35 @@
|
||||
using System.Dynamic;
|
||||
|
||||
namespace Elsa.Workflows.IntegrationTests.Scenarios.JavaScriptNativeVariables;
|
||||
|
||||
public class Data
|
||||
{
|
||||
public static ExpandoObject CreateCustomerModel()
|
||||
{
|
||||
dynamic customer = new ExpandoObject();
|
||||
customer.Name = "John Doe";
|
||||
customer.Age = 42;
|
||||
customer.Orders = new[]
|
||||
{
|
||||
new
|
||||
{
|
||||
Id = 1,
|
||||
Product = "Apple",
|
||||
Price = 10
|
||||
},
|
||||
new
|
||||
{
|
||||
Id = 3,
|
||||
Product = "Orange",
|
||||
Price = 30
|
||||
},
|
||||
new
|
||||
{
|
||||
Id = 2,
|
||||
Product = "Banana",
|
||||
Price = 20
|
||||
}
|
||||
};
|
||||
return customer;
|
||||
}
|
||||
}
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
using Elsa.Extensions;
|
||||
using Elsa.Testing.Shared;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace Elsa.Workflows.IntegrationTests.Scenarios.JavaScriptNativeVariables;
|
||||
|
||||
public class Tests
|
||||
{
|
||||
private readonly IWorkflowRunner _workflowRunner;
|
||||
private readonly CapturingTextWriter _capturingTextWriter = new();
|
||||
private readonly IWorkflowBuilderFactory _workflowBuilderFactory;
|
||||
private readonly IServiceProvider _services;
|
||||
|
||||
public Tests(ITestOutputHelper testOutputHelper)
|
||||
{
|
||||
_services = new TestApplicationBuilder(testOutputHelper)
|
||||
.WithCapturingTextWriter(_capturingTextWriter)
|
||||
.ConfigureElsa(elsa => elsa.UseJavaScript())
|
||||
.Build();
|
||||
_workflowBuilderFactory = _services.GetRequiredService<IWorkflowBuilderFactory>();
|
||||
_workflowRunner = _services.GetRequiredService<IWorkflowRunner>();
|
||||
}
|
||||
|
||||
[Fact(DisplayName = "The JavaScript activity can access and modify native variables")]
|
||||
public async Task Test1()
|
||||
{
|
||||
await _services.PopulateRegistriesAsync();
|
||||
var workflow = await _workflowBuilderFactory.CreateBuilder().BuildWorkflowAsync<JavaScriptWorkflow>();
|
||||
await _workflowRunner.RunAsync(workflow);
|
||||
var line1 = _capturingTextWriter.Lines.ToList().ElementAt(0);
|
||||
var line2 = _capturingTextWriter.Lines.ToList().ElementAt(1);
|
||||
var line3 = _capturingTextWriter.Lines.ToList().ElementAt(2);
|
||||
Assert.Equal("Jane Doe", line1);
|
||||
Assert.Equal("Apple, Banana, Orange", line2);
|
||||
Assert.Equal("jane.doe@acme.com", line3);
|
||||
}
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
using Elsa.JavaScript.Activities;
|
||||
using Elsa.JavaScript.Models;
|
||||
using Elsa.Workflows.Activities;
|
||||
|
||||
namespace Elsa.Workflows.IntegrationTests.Scenarios.JavaScriptNativeVariables;
|
||||
|
||||
public class JavaScriptWorkflow : WorkflowBase
|
||||
{
|
||||
protected override void Build(IWorkflowBuilder workflow)
|
||||
{
|
||||
workflow.WithDefinitionId(Guid.NewGuid().ToString());
|
||||
var customerVariable = workflow.WithVariable("customer", Data.CreateCustomerModel());
|
||||
|
||||
workflow.Root = new Sequence
|
||||
{
|
||||
Activities =
|
||||
{
|
||||
new RunJavaScript
|
||||
{
|
||||
Script = new("""
|
||||
const customer = variables.customer;
|
||||
customer.Name = 'Jane Doe';
|
||||
customer.Orders.sort((a, b) => a.Product.localeCompare(b.Product));
|
||||
variables.customer = {...customer, Email: 'jane.doe@acme.com'};
|
||||
""")
|
||||
},
|
||||
new WriteLine(JavaScriptExpression.Create("variables.customer.Name")),
|
||||
new WriteLine(JavaScriptExpression.Create("variables.customer.Orders.map(x => x.Product).join(', ')")),
|
||||
new WriteLine(JavaScriptExpression.Create("variables.customer.Email"))
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user