-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move server tests to correct test folder
- Loading branch information
Showing
3 changed files
with
236 additions
and
276 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
defmodule Yex.UndoServerTest do | ||
use ExUnit.Case | ||
alias Yex.{Doc, Text, UndoManager} | ||
doctest Yex.UndoManager | ||
|
||
setup do | ||
doc = Doc.new() | ||
text = Doc.get_text(doc, "text") | ||
map = Doc.get_map(doc, "map") | ||
|
||
if Process.whereis(:test_observer_process) do | ||
Process.unregister(:test_observer_process) | ||
end | ||
Process.register(self(), :test_observer_process) | ||
|
||
on_exit(fn -> | ||
if Process.whereis(:test_observer_process) do | ||
Process.unregister(:test_observer_process) | ||
end | ||
end) | ||
|
||
# Return these as the test context | ||
{:ok, doc: doc, text: text, map: map} | ||
end | ||
|
||
defmodule TestObserver do | ||
use Yex.UndoServer | ||
|
||
def handle_stack_item_added(stack_item, state) do | ||
{:ok, Map.put(stack_item, "test_value", "added"), state} | ||
end | ||
|
||
def handle_stack_item_popped(state) do | ||
send(:test_observer_process, {:stack_item_popped, %{"test_value" => "added"}}) | ||
{:ok, state} | ||
end | ||
end | ||
|
||
defmodule SecondObserver do | ||
use Yex.UndoServer | ||
|
||
def handle_stack_item_added(stack_item, state) do | ||
{:ok, Map.put(stack_item, "test_value", "added"), state} | ||
end | ||
|
||
def handle_stack_item_popped(state) do | ||
send(:test_observer_process, {:stack_item_popped, %{"test_value" => "added"}}) | ||
{:ok, state} | ||
end | ||
end | ||
|
||
defmodule IgnoringObserver do | ||
use Yex.UndoServer | ||
|
||
def handle_stack_item_added(_stack_item, state) do | ||
{:ignore, state} | ||
end | ||
|
||
def handle_stack_item_popped(state) do | ||
send(:test_observer_process, {:stack_item_popped, :ok}) | ||
{:ok, state} | ||
end | ||
end | ||
|
||
|
||
test "observer receives callbacks and can modify stack items", %{doc: doc, text: text} do | ||
|
||
undo_manager = UndoManager.new(doc, text) | ||
UndoManager.add_observer(undo_manager, TestObserver) | ||
|
||
# Make a change that should trigger the observer | ||
Text.insert(text, 0, "Hello") | ||
|
||
# Undo should trigger the popped callback with metadata | ||
UndoManager.undo(undo_manager) | ||
|
||
# Verify we received the meta information with our test value | ||
assert_receive {:stack_item_popped, %{"test_value" => "added"}} | ||
end | ||
|
||
test "observer can ignore stack items", %{doc: doc, text: text} do | ||
undo_manager = UndoManager.new(doc, text) | ||
UndoManager.add_observer(undo_manager, IgnoringObserver) | ||
|
||
# Make and undo changes - should work without error even though observer ignores | ||
Text.insert(text, 0, "Hello") | ||
assert Text.to_string(text) == "Hello" | ||
UndoManager.undo(undo_manager) | ||
assert Text.to_string(text) == "" | ||
end | ||
|
||
test "multiple observers can be added", %{doc: doc, text: text} do | ||
undo_manager = UndoManager.new(doc, text) | ||
|
||
# Add observers - prefix with underscore since we don't use the return values | ||
_pid1 = UndoManager.add_observer(undo_manager, TestObserver) | ||
_pid2 = UndoManager.add_observer(undo_manager, TestObserver) | ||
|
||
Text.insert(text, 0, "hello") | ||
UndoManager.undo(undo_manager) | ||
|
||
assert_receive {:stack_item_popped, %{"test_value" => "added"}} | ||
end | ||
end |
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
Oops, something went wrong.