   afterEach ->
     @$container.remove()
 
-  it "loads site.js from another server", (done) ->
-    widget = new TestWidget(el: $(".epages-shop-widget:first"))
 
-    widgetLoaded = ->
-      widget.hasCategoryList() && widget.hasProductList()
+  it "loads categories, product details and variations", (done) ->
+    widget = new TestWidget
+      el: $(".epages-shop-widget:first")
 
-    waitFor widgetLoaded, ->
-      expect(widget.hasCategoryOption("Shoes")).toBeTruthy()
+    waitUntil widget.isReady, ->
+      expect( widget.hasCategoryOption("Shoes") ).toBeTruthy()
       widget.selectCategory("Shoes")
 
-      categoryLoaded = ->
-        widget.hasProduct("Meindl RFS Tibet")
-
-      waitFor categoryLoaded, ->
+      waitUntil widget.hasProduct, "Meindl RFS Tibet", ->
         widget.productLink("Meindl RFS Tibet").click()
 
-        waitFor widget.variationsLoaded, ->
+        waitUntil widget.variationsLoaded, ->
           expect( $("label[for='epages-shop-variation-USSize']").is(':visible') ).toBeTruthy()
           done()
-

  constructor: (options) ->
    @$el = options.el

  $: (selector) ->
    @$el.find(selector)

  hasCategoryList: ->
    @$("option").length > 0

  hasProductList: ->
    @$(".epages-shop-product").length > 0

  variationsLoaded: ->
    $(".pico-content").find(".epages-shop-variation").length > 0

  hasCategoryOption: (name) ->
    @$("option:contains(#{name})").length > 0

  selectCategory: (name) ->
    value = @$("option:contains(#{name})").val()
    @$("select").val(value).change()

  hasProduct: (name) ->
    @$(".epages-shop-product-name:contains(#{name})").length == 1

  productLink: (name) ->
    @$("a .epages-shop-product-name:contains(#{name})")



waitUntil = (condition, args..., next) ->
  interval = setInterval ->
    if condition(args...)
      clearInterval(interval)
      next()
  , 10
